JAVA 访问别人的接口。

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);

            hr.addParam("sex","男");


        //返回值为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");
         * */
    }

}

猜你喜欢

转载自blog.csdn.net/Liuyooer/article/details/70755909