【发布时间】:2015-04-12 13:27:29
【问题描述】:
我正在使用此代码将请求从 Android 发送到 Spring 服务器。 Spring中的方法必须返回一个String,但是不起作用:
idstringtoparse=CustomHttpClient.executeHttpPost(urlGetUserIdByUsername, params);
作为方法executeHttpPost这样:
public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
try{
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters,"UTF-8");
formEntity.setContentEncoding(HTTP.UTF_8);
request.setEntity(formEntity);
request.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
String result = sb.toString();
return result;
}
}
在服务端,代码是这样的:
@RequestMapping ("usuario/getIdUserByUsername")
@ResponseBody
public Long getUserIdByUsername(@RequestParam String username){
Long id=(long)usuarioService.getUserIdByUsername(username);
usuarioService.getUserIdByUsername(username);
return id;
}
这样,我收到一个 406 错误,说“此请求标识的资源只能生成具有根据请求“接受”标头不可接受的特征的响应”
这有点奇怪,因为在此之前我在我的应用程序中使用了相同的方法来记录服务器,并且它运行良好。
【问题讨论】:
标签: android spring http-headers