【问题标题】:How to send json object as request parameter in a url using HttpsURLConnection in android如何在android中使用HttpURLConnection在url中发送json对象作为请求参数
【发布时间】:2016-06-01 10:28:35
【问题描述】:

我的网址为https://www.xyz.in/ws/bt,请求参数为令牌、块请求和格式。 示例 JSON “blockrequest” 字符串是


{"source\":\"1492\",\"destination\":\"1406\",\"availableTripId\":\"100008417320611112\",\"boardingPointId\":\"1129224\",\"inventoryItems\":[{\"seatName\":\"21\",\"ladiesSeat\":\"false\",\"passenger\":{\"name\":\"passenger_name_1\",\"title\":\"MR\",\"gender\":\"MALE\",\"age\":\"23\",\"primary\":true,\"idType\":\"PANCARD\",\"email\":\"pass_name@domain_name.com\",\"idNumber\":\"BEPS1111B\",\"address\":\"passenger_address\",\"mobile\":\"xxxxxxxxxx\"},\"fare\":\"320.00\"},{\"seatName\":\"22\",\"ladiesSeat\":\"true\",\"passenger\":{\"name\":\"passenger_name_1\",\"title\":\"MS\",\"gender\":\"FEMALE\",\"age\":\"23\",\"primary\":false,\"idType\":\"\",\"email\":\"\",\"idNumber\":\"\",\"address\":\"\",\"mobile\":\"\"},\"fare\":\"320.00\"}]}

如何使用 HttpsURLConnection 在 url 中将这些数据作为 请求参数 发送。

【问题讨论】:

  • “作为请求参数”是指 HTTP GET 请求?
  • 否,通过 HTTPS POST 请求。
  • 它对我不起作用。我需要使用 HttpsURLConnection 的 Https POST 请求。

标签: android json httpsurlconnection


【解决方案1】:

如果您使用 Apache HTTP 客户端。这是一个代码示例

protected void send(final String json) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;

                try {
                    HttpPost post = new HttpPost(URL);
                    StringEntity se = new StringEntity( json.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    /*Checking response */
                    if(response!=null){
                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    createDialog("Error", "Cannot Estabilish Connection");
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();      
    }

这是上面示例代码中的 imp 行:

StringEntity se = new StringEntity( json.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);  

试试这个。我希望它有效。

【讨论】:

  • Apache HTTP 客户端在 API 23 (Android 6) 中已被弃用。
  • 改用 Volley 库
  • 是的,正确。使用截击或改造。这样您就可以轻松地在请求中发送 json 正文。
【解决方案2】:

你可以这样做:

URL url = new URL(yourUrl);
byte[] postData = yourJsonString.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);

conn.getOutputStream().write(postDataBytes);

(要读取响应,请使用连接的getInputStream() 方法)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2019-04-09
    • 1970-01-01
    • 2020-06-17
    相关资源
    最近更新 更多