2014-03-26 76 views
0

我想在我的android應用程序內使用http post方法發佈一些json數據,但是我似乎無法得到它的工作,字符串是建設良好,它的工作原理,如果我測試使用谷歌Chrome插件高級休息客戶端。我不是JSON最強的,因此它是一個字符串而不是JSON對象。郵政請求不會執行。在此先感謝HTTP post JSON java

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/[email protected]"); 

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()); 
+0

究竟是什麼問題? –

+0

設置內容類型。 –

+0

它不執行發佈請求 – user3456401

回答

0

什麼是您從Http客戶端發佈後回來的響應代碼。

對於一個成功的Http應該是200。其他方面還有一些問題。

您正在將JSON數據發佈到URL。哪個應用程序正在從URL中讀取這些數據。是否有其他應用程序正在從請求中讀取JSON數據?檢查並看看。

另請檢查Http Post請求的請求標頭。如果你正確設置所有的請求頭。

+0

我根本沒有收到任何響應,它幾乎好像根本沒有發佈發佈請求 – user3456401

+0

您的意思是Response對象是否爲空?如果不是,它有什麼狀態? –

+0

是的響應是空的,就好像代碼從未被觸發 – user3456401