欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android开发之HttpClient网络请求以Json方式提交Post请求代码

程序员文章站 2022-07-14 17:57:21
...
public class PayHttpUtils {
    /**
     * @param url 请求的网址
     */
    public static String GetSingleCabCollect(String url) {
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonParam = new JSONObject();
        try {
            jsonParam.put("loginName", "admin");
            jsonParam.put("loginPwd", "admin");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        StringEntity entity = null;//解决中文乱码问题
        try {
            entity = new StringEntity(jsonParam.toString(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (entity != null) {
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
        }
        httpPost.setEntity(entity);
        HttpClient httpClient = new DefaultHttpClient();
        // 获取HttpResponse实例
        HttpResponse httpResp = null;
        try {
            httpResp = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 判断是够请求成功
        if (httpResp != null) {
            if (httpResp.getStatusLine().getStatusCode() == 200) {
                // 获取返回的数据
                String result = null;
                try {
                    result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
                    Log.e("HttpPost方式请求成功,返回数据如下:", result);
                    return result;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("打印数据", "HttpPost方式请求失败" + httpResp.getStatusLine().getStatusCode());
            }
        }
        return null;
    }
}

如何调用呢?

 final String result = PayHttpUtils.GetSingleCabCollect("http://10.168.65.144:8012/wallet/admin/login");

我们来看下返回值:

{
    "code": "0000",
    "data": {
        "createDateTime": "2018-08-07T09:34:33.000+0000",
        "createUserId": "admin",
        "deleteFlag": 1,
        "deptId": 1000,
        "email": "[email protected]",
        "jwtToken": "eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNpMjssKAjEMRf8l6w7Mq7UzO0H34h-0kywq86LtoCL-u01B6Ca5NyeX5AOP6GCEwVpTSzlULcqp6lHLSltlK2W7ydQntL1SICAcNi0bXNyanAuhdCbC2MhOtbqXjRRAr_0_0A0PdvILJ45AXnA5I-Z-RRezuNAskPaYC1PumbJg6reZcmHKPVMWTPlC-sptayg17xY2RwrPSb5-p0Dx9kxX3RQTMP4N3x8AAAD__w.VqS3ISzoXCuNq-WSdHywECo54LOTwxPn5IJbRZDjKsFlBo39v89Pzt6LofkcGvn8w83O_CcsWbf9YSPCDKFhqQ",
        "keyId": 1,
        "loginName": "admin",
        "loginPwd": "21232f297a57a5a743894a0e4a801fc3",
        "modifyDateTime": "2018-08-01T06:33:27.000+0000",
        "name": "管理员",
        "page": {
            "current": 1,
            "size": 10
        },
        "phone": "",
        "sysDept": {
            "children": [],
            "deptId": "1000",
            "deptName": "下一页支付",
            "keyId": 1,
            "parentId": "0000",
            "remark": "",
            "viewLevelNo": "1"
        }
    },
    "msg": "登陆成功"
}