java - apache httppost如何设置内容: which have name value pair pointing to another set of name value pair

标签 java android apache 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));

如何实现嵌套

最佳答案

请参阅this问题,因为它有几个答案应该可以帮助你。以下是答案代码的简短 fragment :

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

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

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

           }

关于java - apache httppost如何设置内容: which have name value pair pointing to another set of name value pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7019133/

相关文章:

android - 由 BOOT_COMPLETE 启动的服务在系统终止后会发生什么?

php - 如何将 PHP 查询字符串转换为基于斜线的 URL?

WSL2 中的 Apache 不像 localhost 那样工作

java - 无法使用spring的SchedulerFactoryBean配置quartz线程

javascript - 从 JavaScript 调用 Java 代码(Spring boot)

javascript - 未处理的 JS 异常 : Requiring unknown module "9"

Android 模拟器不会在 Android Studio 中关闭(我在 AVD Manager 的“操作”选项卡下没有看到 "Stop")

php - SCP 命令行和 PHP exec() 不做同样的事情

java - 在 reducer 中迭代自定义可写组件的问题

java - Camel 和ActiveMQ : Failed to resolve endpoint