簡體   English   中英

Android中對HTTP POST請求的意外響應

[英]Unexpected response to HTTP POST request in Android

我最近在Android上通過http POST請求發送/接收數據時遇到了一個奇怪的問題。

我在設置Fiddler來監視我的Android應用程序與服務器之間的通信時遇到了困難,因此我創建了一個簡單的Web表單來模擬POST請求。

<form action="http://www.my.server.org/my_script.php" method="POST">
  <input name="deviceID" type="text" width=30> Device ID <br>
  <input name="lang" type="text" width=30> Language (en / cs) <br>
  <input name="lastUpdated" type="text" width=30> Last Updated (yyyy-MM-dd hh:mm) <br>
  <button type="submit">Send</button>
</form>

當我使用此表單發送請求時,響應將返回200 OK狀態代碼和所需的XML文件。

我認為這將等同於我在Android應用程序中擁有的Java代碼。

private static final String POST_PARAM_LAST_UPDATED = "lastUpdated";
private static final String POST_PARAM_DEVICE_ID = "deviceId";
private static final String POST_PARAM_LANG = "lang";

...

// Create a POST Header and add POST Parameters
HttpPost httpPost = new HttpPost(URL_ARTICLES);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3);
postParameters.add(new BasicNameValuePair(POST_PARAM_DEVICE_ID, deviceId));
postParameters.add(new BasicNameValuePair(POST_PARAM_LANG, lang));
postParameters.add(new BasicNameValuePair(POST_PARAM_LAST_UPDATED, lastUpdated));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

// Create new HttpClient and execute HTTP Post Request
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);

// Get and parse the response
List<Article> parsedArticles = new ArrayList<Article>();
HttpEntity entity = response.getEntity();
if (entity != null) {
  parsedArticles = Parser.parseArticles(entity.getContent());
}

但是,即使我輸入了相同的參數值(與在Web表單中輸入的參數值一樣),在這種情況下的響應仍然是204 NO CONTENT並且顯然沒有XML文件。

有人可以告訴我這兩種方法為什么不等效並且響應不同嗎? 是編碼方面的東西還是我想念的東西?

不幸的是,我無法訪問服務器,而且我無法調試Android的傳出和傳入數據,因為Fiddler和我與PC相連的仿真器/設備拒絕合作。

我也想知道是否應該使用AndroidHttpClient而不是DefaultHttpClient但是我認為在這種情況下它不會更改任何內容。

提前致謝!

由於Maxims的評論,我發現了問題所在。

這是POST_PARAM_DEVICE_ID常量中的一個愚蠢的小寫字母。 它的值為“ deviceId”(並且應為Web表單中的“ deviceID”)。

好吧,我的開發人員,定義字符串鍵時要注意-區分大小寫!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM