目錄
1. JAVA WEB接口開發簡述
1.1. 基本了解
1.2. 提供接口
1.3. 調用接口
1. JAVA WEB接口開發簡述
1.1. 基本了解
當我們想去訪問其他網站的接口時候,而又不想要登錄驗證等操作,那么可以選擇使用平台提供的接口進行調用得到我們需要的信息。比如說,一個網站A有一個接口路徑: http://192.168.10.119:8080/xmq/webservice/menu/search?ak=APP00013&token=yq6ZaljwYMa1x83r0hSHVhQ45DA%3D
當我們需要調用這個接口的時候就要滿足ak參數以及token參數。這個時候,我們需要去拼接這樣的一個url,然后調用平台提供的jar包或者其他的工具去獲取信息。
1.2. 提供接口
確保網站A提供的調用接口可以使用,這里開發接口的時候,需要定義一些規則,比如具體的返回數據定義,狀態碼定義等等,以便調用更明了。具體開發要根據實際情況來決定。
1.3. 調用接口
這樣的接口我們可能用到這些jar包,如下圖:

當然還有json等相關的jar包,這個需要根據調用的網站來確定需要哪些具體的jar包。
常用到的類如HttpClient、HttpGet、HttpPost、HttpDelete等。
簡單調用HttpGet:
1 protected HttpClient c; 2 3 HttpGet get = new HttpGet(url); 4 5 HttpResponse response = c.execute(get);
簡單調用HttpPost:
1 HttpPost post = new HttpPost(url); 2 3 StringEntity entity = new StringEntity(json, ContentType.create("text/plain", "UTF-8")); 4 5 post.setEntity(entity); 6 7 response = c.execute(post);
簡單調用HttpDelete:
1 HttpDelete delete = new HttpDelete(url); 2 3 HttpResponse response = c.execute(delete);
調用文件流和參數(比如有的接口是文件上傳的情況):
//定義調用參數 String private_key = "fcea920f7412b5da7be0cf42b8c93759"; String timestampP=getTimestamp()+""; String accountP = "123456"; String projidP = "10000"; String typeP = "1"; String signP=accountP+private_key+timestampP+projidP+typeP; //設置請求參數 StringBody account = new StringBody(accountP); StringBody timestamp = new StringBody(timestampP); StringBody sign = new StringBody(getMD5Str(signP)); StringBody projid = new StringBody(projidP); StringBody type = new StringBody(typeP); FileBody fb = new FileBody(new File("C:/xmq.dwg")); //設置請求參數類型 MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("account", account); reqEntity.addPart("timestamp", timestamp); reqEntity.addPart("projid", projid); reqEntity.addPart("sign", sign); reqEntity.addPart("type", type); reqEntity.addPart("file", fb); String url = "http://122.22.11.50:9090/UploadFile"; HttpPost httpPost = new HttpPost(url); httpPost.setEntity(reqEntity);
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 120000);//設置連接超時
HttpConnectionParams.setSoTimeout(httpParams, 120000);//設置請求超時 HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpPost); System.out.println("調用結果Data:" + CameraUtil.getData(response));
//附件上傳接口,需要注意接口提供者的服務器有沒有限制文件上傳的大小、請求超時的時間等;
//我就碰到這樣的情況,大附件怎么都上傳不了,我找了很久原因,發現接口提供者IIS服務限制了附件上傳的大小。。。
調用文件流(比如有的接口是文件下載的情況):
import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class FileDown { public static void main(String[] args) { // 根據已有的url連接下載文件 String urlStr = "http://117.27.145.20:8088/2017010915281017322.edc"; String fileName = "C:/Users/Administrator/Desktop/work/wjxt_文件系統/a.edc"; downloadFromUrl(urlStr, fileName); } /** * 根據urlStr下載流,下載文件到指定fileName * * @param urlStr * @param fileName */ public static void downloadFromUrl(String urlStr, String fileName) { // 構造URL URL url; try { url = new URL(urlStr); // 打開連接 URLConnection con = url.openConnection(); // 輸入流 InputStream is = con.getInputStream(); // 1K的數據緩沖 byte[] bs = new byte[1024]; // 讀取到的數據長度 int len; // 輸出的文件流s OutputStream os = new FileOutputStream(new File(fileName)); // 開始讀取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完畢,關閉所有鏈接 os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } }
我們可以對response進行處理,如:
1 //200為成功狀態碼 2 3 if(response.getStatusLine().getStatusCode() == 200){ 4 5 String responseText = null; 6 7 try { 8 9 responseText = EntityUtils.toString(response.getEntity() , "UTF-8"); 10 11 } catch (ParseException e) { 12 13 e.getMessage(); 14 15 } catch (IOException e) { 16 17 e.getMessage(); 18 19 } 20 21 //返回數據處理responseText 22 23 //一般是json數據格式,根據實際需求處理 24 25 }else{ 26 27 //異常信息response.getStatusLine().getStatusCode(); 28 29 }
