开发者

apache httppost how to set content : which have name value pair pointing to another set of name value pair

For example if we need to send content which is in this format , how do we do it {"name1":[{"name11":"value11"},{"name11":"value12"},{"name11":"value13"}],"name2":value2}

I know how to set the basic kind {"name1":"value1","name2":value2}

NameValuePair[] nameValuePairs = new NameValuePair[2];   
            nameVa开发者_如何学GoluePairs[0]= new BasicNameValuePair("name1", "value1");
            nameValuePairs[1] = new BasicNameValuePair("name2", value2);

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

How can we achieve nesting


Please see this question as it has a couple of answers that should help you. Here is a brief snippet from the answers code:

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

The other answer says you can do something like this:

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");
                }

           }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜