将json对象发送到android中的webservice

Posted

技术标签:

【中文标题】将json对象发送到android中的webservice【英文标题】:sending json object to webservice in android 【发布时间】:2013-10-15 00:52:28 【问题描述】:

我是 android 的新手,并试图通过发送 json 对象来使用 .net weservice。

我正在创建一个 json 对象并向其添加所有参数,例如

 JSONObject json = new JSONObject();

   json.put("name","koli"); 
   json.put("email","asdf@email.com");

并在 AsyncTask 类的 doInBackground 方法中调用请求

String response = HttpClient.SendHttpPost(params[0],json);

这就是我尝试提出请求的方式

public static String SendHttpPost(String URL, JSONObject jsonData) 

        Log.d("jsonData", ""+jsonData);
        StringBuilder stringBuilder = new StringBuilder();
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);
        httpPostRequest.setHeader("User-Agent", "com.altaver.android_PostJson2");
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-Type", "application/json");
       StringEntity se = null;
        try 
            se = new StringEntity(jsonData.toString(),"UTF-8");

            se.setContentType("application/json");
         catch (UnsupportedEncodingException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
        httpPostRequest.setEntity(se);
        HttpResponse response = null;
        try 
            response = client.execute(httpPostRequest);
         catch (ClientProtocolException e) 
            // TODO Auto-generated catch block

            e.printStackTrace();
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        

上面的方法是做什么的

StringEntity se = null;
            try 
                se = new StringEntity(jsonData.toString(),"UTF-8");

                se.setContentType("application/json");
             catch (UnsupportedEncodingException e) 

这个 sn-p 就可以了。

为什么我无法发送我的 jsonobject?

谢谢:)

【问题讨论】:

代码正确,有什么问题? @shem JSON 对象,我从 android 发送的在服务端为 NULL 在 Log 中打印 jsonData 是否正常? @shem 是的,这是正确的 【参考方案1】:

在你的 doInBackground 中试试这个..

JSONObject jObjOut = null;

                try 
                    HttpPost request = new HttpPost(url);
                    JSONObject returnedJObject;
                    returnedJObject = new JSONObject(JSONdata.toString());
                    JSONStringer json = new JSONStringer();
                    StringBuilder sb=new StringBuilder();


                    if (returnedJObject!=null)
                    
                        Iterator<String> itKeys = returnedJObject.keys();
                        if(itKeys.hasNext())
                            json.object();
                        while (itKeys.hasNext())
                        
                            String k=itKeys.next();
                            json.key(k).value(returnedJObject.get(k));
                            //Log.e("keys "+k,"value "+returnedJObject.get(k).toString());
                                     
                    
                   json.endObject();


                   StringEntity entity;

                   entity = new StringEntity(json.toString());

                   entity.setContentType("application/json;charset=UTF-8");
                  // entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                   request.setHeader("Accept", "application/json");
                   request.setEntity(entity);

                   HttpResponse response =null;
                   DefaultHttpClient httpClient = new DefaultHttpClient();

                   HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000);
                   HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),50000);

                   response = httpClient.execute(request);

                   InputStream in;
                   in = response.getEntity().getContent();

                   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                   String line = null;
                   while((line = reader.readLine()) != null)
                       sb.append(line);
                   


                   return new JSONObject(sb.toString());  

【讨论】:

【参考方案2】:

尝试更换:

se = new StringEntity(jsonData.toString(),"UTF-8");

到这里:

se = new StringEntity(jsonData.toString());

【讨论】:

以上是关于将json对象发送到android中的webservice的主要内容,如果未能解决你的问题,请参考以下文章

如何将java json对象数据发送到android api请求

如何将json从android发送到php?

如何从android中的json对象呈现和选择联系人?

我想使用 json 将 Listview 数据发送到 Android 中的另一个 Activity

如何使用 REST 将 Json 字符串发送到 android 参数中的 java webservice?

在 Android 中解码 jpeg