我正在编写一个连接到我自己的 Jersey 休息客户端的 android 应用程序。HTTP get 命令工作正常,但我在尝试向服务器发送内容的 POST 时遇到问题。我收到了 405 发回,因此服务器似乎无法将请求与资源方法匹配。有什么想法吗?下面的测试代码...
休息服务器
@PUT
@Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
public Response putResponse(MultiPart multiPart) {
System.out.println(multiPart.getBodyParts());
return null;
}
安卓客户端
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(URL + "responses");
request.addHeader("Content-Type", "multipart/mixed");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("Testpart1", new StringBody("<testxml></testxml>"));
entity.addPart("image1", new StringBody("imagedata1"));
request.setEntity(entity);
request.addHeader("deviceId", deviceId);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
String result = httpclient.execute(request, handler);
Log.i("tag", result);
return result;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
return null;
TCPMon 流量显示以下 POST /Maintenance_Server/rest/responses HTTP/1.1 Content-Type: multipart/mixed deviceId: xxxxx Content-Length: 244 Host: 127.0.0.1:12345 Connection: Keep-Alive User-Agent: Apache-HttpClient/不可用(Java 1.4)
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="Testpart1"
<testxml></testxml>
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="image1"
imagedata1
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7--
谢谢
标记