我正在用 android 编写一个应用程序,我正在向 java servlet 发送一个 json 对象。
这是安卓代码:
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
JSONObject jsonObjectToPass = returnJsonStringObject();
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("jsonGPSParameter",jsonObjectToPass.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");
post.setEntity(entity);
response = httpClient.execute(post);
这是java servlet代码
request.getParameter("jsonGPSParameter");
问题在于 getParameter() 方法返回 null。 我无法发现问题。 如果有人可以帮助我,那将是一个很大的帮助。
谢谢
最佳答案
为了让代码正常工作,我必须从 android 代码中删除下一行:
entity.setContentType("application/json");
post.setHeader("Content-type", "application/json");
关于android - 当我想在 java servlet 中访问他时,从 android 应用程序发送的 JSON 对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074934/