1

我正在编写一个连接到我自己的 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--

谢谢

标记

4

1 回答 1

0

您正在发送 HTTP POST,但在服务器端您声明了一个仅用于 HTTP PUT 的处理程序。因此,它无法将 POST 与任何方法匹配,因此出现 405。将资源方法上的注释从 @PUT 更改为 @POST 或由客户端发送 HTTP PUT 而不是 POST。

于 2012-06-16T14:04:40.103 回答