简体   繁体   English

HTTP发布JSON Java

[英]HTTP post JSON java

I'm trying to post some json data using a http post method within my android application however i cannot seem to get it to work, the string is building fine and it works if i test using google chrome addon advanced rest client. 我正在尝试在我的android应用程序中使用http post方法发布一些json数据,但是我似乎无法使其正常工作,该字符串正在正常构建,并且如果我使用google chrome addon高级其余客户端进行测试,则可以正常工作。 I'm not the strongest with JSON hence why it is a string and not a JSON object. 我不是JSON最强的人,因此为什么它是字符串而不是JSON对象。 The Post request does not execute. 发布请求不会执行。 Thanks in advance 提前致谢

    String json = "{\"data\": [";
for (String tweet : tweetContent)
{
    json = json + "{\"text\": \"" + tweet + "\", \"query\": \"" + SearchTerm + "\", \"topic\": \"movies\"},";
}
json = json.substring(0, json.length() - 1);
json = json + "]}";

Log.i("matt", json);





// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.sentiment140.com/api/bulkClassifyJson?appid=matt-43@hotmail.com");

StringEntity entity = new StringEntity(json, HTTP.UTF_8);


httppost.setEntity(entity);

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

    String responseBody = EntityUtils.toString(response.getEntity());
    Log.i(LOG_TAG, responseBody);
    sentiments.add(responseBody.toString());

what is the response code that you are getting back after posting from the Http client. 从Http客户端发布后返回的响应代码是什么。

It should be 200 for a successful Http post. 一个成功的Http帖子应该是200。 Other wise there is some issue. 否则,会有一些问题。

You are posting the JSON data to the URL. 您正在将JSON数据发布到URL。 which application is reading this data from the URL. 哪个应用程序正在从URL读取此数据。 Is there some servlet on the other application that is reading JSON data fromrequest. 在另一个从请求读取JSON数据的应用程序上是否有servlet。 check that and see. 检查一下,看看。

Also check the request headers for the Http Post request. 还要检查Http Post请求的请求标头。 if you are setting all the request headers properly. 如果您正确设置了所有请求标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM