【问题标题】:Unable to post JSON data using HttpPost无法使用 HttpPost 发布 JSON 数据
【发布时间】:2015-03-23 07:46:31
【问题描述】:

我正在尝试将 JSON 数据发布到我的 API。但执行后我得到以下结果:

{"name":"Corporate","addr":"Unknown","area":"Unknown","cityId":10,"phone":"--","fax":"--","wooqStoreId":1}]

Response 2 >>{"message":"Blank String","result":"Error","resultCode":"RESULT_CODE_002"}
true

前 2 行显示我的 JSON 字符串和 响应 2 是我收到的消息。当我收到状态码 200 时,这应该是一条成功消息。

public static boolean pushtoAPI(String url, String jsonObject) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpPost request = null;
            HttpResponse response = null;
            String postUrl = getHostUrl() + url;

            try {
                    request = new HttpPost(postUrl);


                    StringEntity postingString = new StringEntity(jsonObject.toString());
                    postingString.setContentType("application/json;charset=UTF-8");
                    postingString.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                                    "application/json;charset=UTF-8"));

                    request.setEntity(postingString);


                    request.setHeader("Content-type", "application/json");


                    String custom_cookie = ConstantUtil.authCookie(ConstantUtil.getLoginJsessionId());
                    request.setHeader("Cookie", custom_cookie);

                    response = client.execute(request);
                    System.out.println("Response 2 >>" + EntityUtils.toString(response.getEntity()));
                    if (response.getStatusLine().getStatusCode() == 200) {
                            System.out.println("true");
                            return true;
                    }
            } catch (ClientProtocolException e) {
                    e.printStackTrace();
            } catch (IOException e) {
                    e.printStackTrace();
            } catch (Exception e) {
            } finally {
                    request.abort();
            }

            return false;
    }

【问题讨论】:

  • 显示服务器端 coe。
  • 为什么是javascript tag
  • 如果您不介意使用外部库,我可以向您展示一种非常简单的方法!!!
  • @RequestMapping(value = "/sdk/manage.do", method = RequestMethod.POST) public ModelAndView Function(@RequestParam(value = "data", required = false) String dataObj, HttpServletRequest request ) { if (null == dataObj || (null != dataObj && "".equals(dataObj.trim()))) { return this.createJsonResponseMessage("Error", "Blank String", RESULT_BLANK); } ..................... }
  • 是的,我不介意使用外部库。

标签: java javascript android httpclient


【解决方案1】:

看起来是服务器端代码问题。

你能说明你在哪里创建这个字符串吗?

"消息":"空白 String","re​​sult":"Error","re​​sultCode":"RESULT_CODE_002"

【讨论】:

  • @RequestMapping(value = "/sdk/manage.do", method = RequestMethod.POST) public ModelAndView Function(@RequestParam(value = "data", required = false) String dataObj, HttpServletRequest request ) { if (null == dataObj || (null != dataObj && "".equals(dataObj.trim()))) { return this.createJsonResponseMessage("Error", "Blank String", RESULT_BLANK); } ..................... }
  • 从代码的 sn-p 来看,在我看来 dataObj 总是为空,因此您的响应就是您收到的响应。
  • 如果您能更正我在此 dataObj 中对应于 jsonstring 的“if 语句”,我将非常感谢您。我无法捕捉到错误
  • 我发现的一件事是:在服务器端,我得到空数据。所以当我在做 client.execute(request);它正在发送空值。但为什么呢?
  • List nameValuePairs = new ArrayList(1); nameValuePairs.add(new BasicNameValuePair("userData", jsonObject.toString()));抢劫你是对的,我的数据对象总是给我 null..这个代码问题解决了谢谢
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2013-08-15
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多