Android HttpURLConnection Post请求

注意事项

Content-Type的配置需要和后台一直,否则后台接收不到数据

1、Content-Type:设置为application/json;charset=UTF-8

connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");//json格式

对应的参数格式为:{a:b,c:d}

2、Content-Type:设置为application/x-www-form-urlencoded

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//网页表单格式

对应的格式为"a=b;c=d"

代码

 /**
     * xx
     *
     * @param token xx的token
     * @param sign  xx的sign
     */
    private void riskManager(final String token, final String sign) {
        //请求
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    // 0.相信证书

                    // 1. 获取访问地址URL
                    URL url = new URL("http://xxx.do");
                    // 2. 创建HttpURLConnection对象
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    // 3. 设置请求参数等
                    // 请求方式
                    connection.setRequestMethod("POST");
                    // 超时时间
                    connection.setConnectTimeout(30000);
                    connection.setReadTimeout(30000);
                    // 设置是否输出
                    connection.setDoOutput(true);
                    // 设置是否读入
                    connection.setDoInput(true);
                    // 设置是否使用缓存
                    connection.setUseCaches(false);
                    // 设置此 HttpURLConnection 实例是否应该自动执行 HTTP 重定向
                    connection.setInstanceFollowRedirects(true);
                    //设置通用的请求属性
                    //connection.setRequestProperty("accept", "/*");
                    //connection.setRequestProperty("connection", "Keep-Alive");
                    //connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
                    //设置cookie
                    //connection.setRequestProperty("Cookie", "JSESSIONID=" + SPUtil.getParam(OcrUploadActivity.this, GxjlPe.sessionId, ""));
                    //connection.setRequestProperty("accessToken", (String) SPUtil.getParam(OcrUploadActivity.this, GxjlPe.accessToken, ""));
                    // 设置使用标准编码格式编码
                    //connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");//json格式
                    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//网页表单格式
                    // 连接
                    connection.connect();
                    // 4. 处理输入输出
                    // 写入参数到请求中

//                    //Content-Type:application/json;charset=UTF-8 对应json格式参数数据
//                    String params = "{" +
//                            "\"memberId\":\"" + member_id + "\"" + "," +
//                            "\"transNo\":\"" + trans_id + "\"" + "," +
//                            "\"proId\":\"" + product_id + "\"" + "," +
//                            "\"token\":\"" + token + "\"" + "," +
//                            "\"sign\":\"" + sign + "\"" +
//                            "}";


                    //Content-Type:application/x-www-form-urlencoded 网页表单格式对应的参数数据
                    Map map = new HashMap();
                    map.put("memberId", member_id);
                    map.put("transNo", trans_id);
                    map.put("proId", product_id);
                    map.put("token", token);
                    map.put("sign", sign);

                    String params = convertStringParamter(map);

                    LogUtils.d("BaoFuPay", "RiskManagerParams:" + params);

                    OutputStream out = connection.getOutputStream();
                    out.write(params.getBytes());
                    out.flush();
                    out.close();
                    // 从连接中读取响应信息
                    String msg = "";
                    int code = connection.getResponseCode();
                    if (code == 200) {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            msg += line + "\n";
                        }
                        reader.close();
                    }
                    // 5. 断开连接
                    connection.disconnect();

                    //loading

                    // 处理结果
                    LogUtils.d("BaoFuPay", "RiskManagerResult:" + msg);
                } catch (Exception e) {
                    //loading
                    Log.e("BaoFuPay", "RiskManager:" + e);
                }
            }
        }).start();


    }

 

你可能感兴趣的:(【,Android,工具封装,】)