使用doPost訪問三方介面
如果我們的app應用需要對接第三方服務介面,可以採用doPost方法,程式碼如下:
//獲取章節詳情資訊
public void testGetChapterDetail() throws IOException {
String postUrl = "http://112.13.170.215:18443/cip-cas/getChapterDetail";
Map<String, String> header = getHeader();
JSONObject paramJson = new JSONObject();
String requestSeq = "12345678123456781234567812345678";
paramJson.put("requestSeq", requestSeq);
paramJson.put("appId", APPID);
paramJson.put("password", getPassword(requestSeq));
paramJson.put("equipmentId", EQUIPMENT_ID);
paramJson.put("userId", "18867103563");
paramJson.put("chapterId", "408959988");//章節ID
paramJson.put("chapterQuality", "2");
paramJson.put("actType", "0");
System.out.println("請求引數:" + paramJson.toString());
String reString = HttpUtils.doPost(postUrl, header, paramJson.toString());
System.out.println(reString);
}
首先,需要postUrl、header和請求引數。其次,呼叫HttpUtils的doPost方法,得到返回的字串,再對字串進行處理。