apache httppost如何设置内容:其中名称值对指向另一组名称值对

Kav*_*tha 5 java apache android http

例如,如果我们需要发送此格式的内容,我们该怎么做{"name1":[{"name11":"value11"},{"name11":"value12"},{"name11": "value13"}], "NAME2":VALUE2}

我知道如何设置基本种类{"name1":"value1","name2":value2}

NameValuePair[] nameValuePairs = new NameValuePair[2];   
            nameValuePairs[0]= new BasicNameValuePair("name1", "value1");
            nameValuePairs[1] = new BasicNameValuePair("name2", value2);

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Run Code Online (Sandbox Code Playgroud)

我们怎样才能实现嵌套

Jac*_*ack 7

请看这个问题,因为它有几个答案可以帮助你.以下是答案代码的简短片段:

HttpPost request = new HttpPost(serverUrl);
request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)

另一个答案说你可以这样做:

protected void sendJson(final String email, final String pwd) {

                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();
                try{
                    HttpPost post = new HttpPost(URL);
                    json.put("email", email);
                    json.put("password", pwd);
                    StringEntity se = new StringEntity( "JSON: " + json.toString());  
                    se.setContentEncoding(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");
                }

           }
Run Code Online (Sandbox Code Playgroud)

  • :)非常感谢,第一个选项对我有用.我只是以我需要的格式创建一个字符串 - 比如嵌套的名称对值并使用了ByteArrayEntity (2认同)