[英]REST webservice doesn't receive Android HttpPost parameters
我有一個Web服務,它接收2個(可選)參數:
<resource path="getLogbookEvents">
<method name="POST">
<request>
<param name="startDate" style="query" type="xs:string"/>
<param name="endDate" style="query" type="xs:string"/>
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
我可以使用HttpPost從Android連接並接收答案,但是Web服務從不接收這兩個參數之一。
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://... method URL ...");
//post.setHeader("media-type", "application/json; charset=UTF-8");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
// entity.setContentEncoding(HTTP.UTF_8);
post.setEntity(entity);
// make POST request to the given URL
HttpResponse httpResponse = httpclient.execute(post);
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
我已經嘗試了HttpParams,JSONObject和nameValuePairs,但沒有任何效果。 我只是一直收到響應,好像沒有指定任何參數一樣。 關於為什么會發生這種情況的其他想法,或者我可以嘗試獲得成功組合的其他想法?
不是直接的答案,請不要直接使用httpclient來調試/修復此問題,您可以:
進行http請求。 它非常容易使用。 相信我。
我終於設法使它起作用。
因此,首先,Web服務肯定存在問題。 我不確定是什么,解決問題也不關我的事,所以我什至不會試圖找出它是什么。
基本上,getLogbookEvents不會接收參數,因為它不會在請求期間內接受任何參數。 查看發送的SoapUI原始數據,我發現它始終在URL內而不是正文內發送參數。
我不知道他們如何使POST方法表現得像GET方法,但就目前情況來看,它們顯然是這樣做的。 用UTF-8格式化我的nameValuePairs並將它們添加到URL是我完全可以使用的(完全向后,不建議使用)的方法:)
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("startDate", "2014-04-18T05:00:00"));
nameValuePairs.add(new BasicNameValuePair("endDate", "2014-04-18T06:00:00"));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
HttpPost post = new HttpPost("... basic URL String ..." + paramString);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.