HttpRequest发送网络请求POST/GET

1、HttpRequest.post


    public static String getView1(JSONObject body) {
        String url = "https://";
        String respStr = null;
        try {
            respStr = HttpRequest.post(url)
                    .header("Content-Type", "application/json")    //消息头,可多个
                    .body(body.toJSONString())     //接收String类型数据
                    .timeout(5000)
                    .execute()
                    .body();
        } catch (HttpException e) {
            return null;
        }

        return respStr;
    }

会返回你需要的数据,类型为String

2、HttpRequest.get

   public static String getView(String url, String type) {
        String respStr = null;
        try {
            respStr = HttpRequest.get(url)
                    .form(type)     //接收String类型数据
                    .timeout(5000)
                    .execute()
                    .body();
        } catch (HttpException e) {
            return null;
        }

        return respStr;
    }

HttpRequest.get获取网络发过来的String类型的数据,如需获取里面的某一参数,需对数据进行解析。解析方式如下:

示例接收到的String类型转JSON后格式:

{
        "cname":"经营情况",
        "key":"manageSituationFlag",
        "status":"2",
        "flag":0,
        "data":{

        }
}

 解析:

String respStr = getView(url,type);
JSONObject jsonObject = JSONObject.parseObject(respStr);
JSONObject data = jsonObject.getJSONObject("data");
Long total = data.getLong("total"); //获取data里面的参数数据,返回某一类型

你可能感兴趣的:(json)