Java 无法使用HttpPost发布JSON数据

Java 无法使用HttpPost发布JSON数据,java,javascript,android,httpclient,Java,Javascript,Android,Httpclient,我正在尝试将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 前两

我正在尝试将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
前两行显示我的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;
    }

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

你能显示你在哪里创建这个字符串吗

“消息”:“空白” 字符串“,”结果“:”错误“,”结果代码“:”结果\u代码\u 002”


显示服务器端coe。为什么使用javascript
tag
。如果您不介意使用外部库,我可以向您展示一种非常简单的方法@RequestMapping(value=“/sdk/manage.do”,method=RequestMethod.POST)公共模型和视图函数(@RequestParam(value=“data”,required=false)字符串dataObj,HttpServletRequest request){if(null==dataObj | |(null)=dataObj&&'。等于(dataObj.trim()){返回此.createJsonResponseMessage(“错误”,“空白字符串”,结果为空);}是的,我不介意使用外部库。@RequestMapping(value=“/sdk/manage.do”,method=RequestMethod.POST)公共模型和视图函数(@RequestParam(value=“data”),required=false)字符串dataObj,HttpServletRequest请求){if(null==dataObj | | |(null!=dataObj&“.equals(dataObj.trim()){返回此.createJsonResponseMessage(“错误”,“空白字符串”,结果为空)}从这段代码中,我发现dataObj总是为null,因此使您的响应成为您收到的响应。如果您能更正我的“if语句”,我将非常感谢您在这个dataObj中,对应于jsonstring。我无法捕捉到错误。我发现的一件事是:在服务器端,我得到的是null数据。因此,当我在client.execute(请求)中执行时,它发送的是null。但是为什么呢?List nameValuePairs=new ArrayList(1);nameValuePairs.add(new BasicNameValuePair)(“userData”,jsonObject.toString());rob u是对的,我的dataobject总是给我空值。通过这段代码,问题解决了,谢谢