JAVA 訪問別人的介面。
阿新 • • 發佈:2018-12-30
public class Test1111 {
public static void main(String[] args) throws Exception {
//這裡寫你要訪問的url地址
String url = "www.baidu.com";
//引數
String param1 ="liu";
String param2 ="youer";
HttpRequest hr = new HttpRequest();//這個類,請看我另外一篇
hr.setURL(url);
//訪問方法為post
hr.setMethod("POST");
hr.addParam("xing",param1);
hr.addParam("ming",param2);
//返回值為String型別的Json字串
String response = hr.Send();
//取值方法如下:
JSONObject ret = JSONObject.fromObject(response);
/**
* 如果是多層的Json,比如
* {"server_response":{"status_code":201,"balance":"1974600.00",""coupons_balance":"0.00"}}
* 具體取值,如下:
* JSONObject jsonObj = JSONObject.fromObject(response);
JSONObject jsonObj1 =jsonObj.getJSONObject("server_response");
Object balance =jsonObj1.get("balance");
Object coupons_balance =jsonObj1.get("coupons_balance");
* */
}
}
public static void main(String[] args) throws Exception {
//這裡寫你要訪問的url地址
String url = "www.baidu.com";
//引數
String param1 ="liu";
String param2 ="youer";
HttpRequest hr = new HttpRequest();//這個類,請看我另外一篇
hr.setURL(url);
//訪問方法為post
hr.setMethod("POST");
hr.addParam("xing",param1);
hr.addParam("ming",param2);
hr.addParam("sex","男");
String response = hr.Send();
//取值方法如下:
JSONObject ret = JSONObject.fromObject(response);
/**
* 如果是多層的Json,比如
* {"server_response":{"status_code":201,"balance":"1974600.00",""coupons_balance":"0.00"}}
* 具體取值,如下:
* JSONObject jsonObj = JSONObject.fromObject(response);
JSONObject jsonObj1 =jsonObj.getJSONObject("server_response");
Object balance =jsonObj1.get("balance");
Object coupons_balance =jsonObj1.get("coupons_balance");
* */
}
}