在Android中使用HttpClient和HttpPost和post参数

我正在为Android应用程序编写代码,这个应用程序需要将数据打包为Json,并将其发布到一个Web服务器,而这个Web服务器反过来也是用json来响应的。

使用GET请求工作正常,但由于某种原因使用POST所有数据似乎被剥夺,服务器不会收到任何东西。

以下是代码片段:

HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); DefaultHttpClient httpClient = new DefaultHttpClient(params); BasicCookieStore cookieStore = new BasicCookieStore(); httpClient.setCookieStore(cookieStore); String uri = JSON_ADDRESS; String result = ""; String username = "user"; String apikey = "something"; String contentType = "application/json"; JSONObject jsonObj = new JSONObject(); try { jsonObj.put("username", username); jsonObj.put("apikey", apikey); } catch (JSONException e) { Log.e(TAG, "JSONException: " + e); } HttpPost httpPost = new HttpPost(uri); List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("json", jsonObj.toString())); HttpGet httpGet = null; try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams); entity.setContentEncoding(HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Content-Type", contentType); httpPost.setHeader("Accept", contentType); } catch (UnsupportedEncodingException e) { Log.e(TAG, "UnsupportedEncodingException: " + e); } try { HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { InputStream is = httpEntity.getContent(); result = StringUtils.convertStreamToString(is); Log.i(TAG, "Result: " + result); } } catch (ClientProtocolException e) { Log.e(TAG, "ClientProtocolException: " + e); } catch (IOException e) { Log.e(TAG, "IOException: " + e); } return result; 

我想我已经遵循了关于如何创build参数和发布它们的一般指导原则,但显然不是。

任何帮助或指向我可以find解决scheme的指针,都非常欢迎在这一点上(花了几个小时才意识到没有发送任何发布数据)。 真正的服务器在Tomcat上运行Wicket,但我也在一个简单的PHP页面上进行了testing,没有任何区别。

你有没有尝试过没有JSON对象,只传递了两个basicnamevaluepairs? 另外,这可能与您的服务器设置有关

更新:这是我使用的一段代码:

 InputStream is = null; ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(connection); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.d("HTTP", "HTTP: OK"); } catch (Exception e) { Log.e("HTTP", "Error in http connection " + e.toString()); } 

您实际上可以通过以下方式将其作为JSON发送:

 // Build the JSON object to pass parameters JSONObject jsonObj = new JSONObject(); jsonObj.put("username", username); jsonObj.put("apikey", apikey); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpPost); 

我刚刚检查过,而且我有和你一样的代码,它可以正常工作。 唯一的区别是我如何填写我的参数列表:

我使用a: ArrayList<BasicNameValuePair> params

并用这种方式填充它:

  params.add(new BasicNameValuePair("apikey", apikey); 

我不使用任何JSONObject发送参数到Web服务。

你有义务使用JSONObject?