gpt4 book ai didi

android - 当我想在 java servlet 中访问他时,从 android 应用程序发送的 JSON 对象为空

转载 作者:行者123 更新时间:2023-11-30 04:09:23 25 4
gpt4 key购买 nike

我正在用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com