1. JAVA WEB接口開發簡述java
1.1. 基本瞭解web
1.2. 提供接口json
1.3. 調用接口服務器
當咱們想去訪問其餘網站的接口時候,而又不想要登陸驗證等操做,那麼能夠選擇使用平臺提供的接口進行調用獲得咱們須要的信息。好比說,一個網站A有一個接口路徑: http://192.168.10.119:8080/xmq/webservice/menu/search?ak=APP00013&token=yq6ZaljwYMa1x83r0hSHVhQ45DA%3D工具
當咱們須要調用這個接口的時候就要知足ak參數以及token參數。這個時候,咱們須要去拼接這樣的一個url,而後調用平臺提供的jar包或者其餘的工具去獲取信息。post
確保網站A提供的調用接口可使用,這裏開發接口的時候,須要定義一些規則,好比具體的返回數據定義,狀態碼定義等等,以便調用更明瞭。具體開發要根據實際狀況來決定。網站
這樣的接口咱們可能用到這些jar包,以下圖:url
固然還有json等相關的jar包,這個須要根據調用的網站來肯定須要哪些具體的jar包。spa
經常使用到的類如HttpClient、HttpGet、HttpPost、HttpDelete等。.net
簡單調用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 }