我正在從我的Android應用程序向服務器發送POST請求。服務器是使用Spring Framework開發的。該請求由服務器接收,但我發送的參數爲空/空(顯示在日誌中)。從Android應用程序接收服務器上的POST請求(Spring Framework)
用於發送POST請求的代碼是:
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
String postMessage = "json String";
HttpPost postMethod=new HttpPost("http://ip:port/event/eventlogs/logs");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("json", postMessage));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
hc.execute(postMethod,res);
我也曾嘗試設置的HttpParams如下,但它也失敗:
HttpParams params = new BasicHttpParams();
params.setParameter("json", postMessage);
postMethod.setParams(params);
接收到該服務器端的代碼請求是:
@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@ModelAttribute("json") String json) {
logger.debug("Received POST request:" + json);
return null;
}
我正在記錄的記錄器消息顯示:
Received POST request:
任何想法我在這裏失蹤?
並在客戶端上指定參數作爲請求的實體。將它們指定爲setParams仍然不起作用。 –
我有完全相同的問題。當我做你說的,我得到org.springframework.web.bind.MissingServletRequestParameterException:必需的整數參數'頻道'不存在。我使用基本名稱valuepair從android客戶端發送參數。你能幫我嗎? – Emilla
@Emilla有可能您的參數可能會丟失,然後將其標記爲required = false。看到這個答案http://stackoverflow.com/a/3466851/867475 – rizzz86