Android客户端连接到Jersey Rest - Post给出405

时间:2012-06-16 13:31:20

标签: android rest jersey

我正在编写一个连接到我自己的Jersey rest客户端的Android应用程序。 HTTP get命令工作正常,但我的POST有问题,我试图向服务器发送一些东西。我得到一个405回送,所以似乎服务器无法匹配请求与资源方法。有什么想法吗?测试代码如下......

REST SERVER

@PUT
@Consumes(MultiPartMediaTypes.MULTIPART_MIXED)
public Response putResponse(MultiPart multiPart) {
    System.out.println(multiPart.getBodyParts());
    return null;
}

ANDROID CLIENT

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 Traffic显示以下内容     POST / Maintenance_Server / rest / responses HTTP / 1.1     内容类型:multipart / mixed     deviceId:xxxxx     内容长度:244     主持人:127.0.0.1:12345     连接:保持活力     User-Agent:Apache-HttpClient / UNAVAILABLE(java 1.4)

--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="Testpart1"

<testxml></testxml>
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="image1"

imagedata1
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7--

由于

标记

1 个答案:

答案 0 :(得分:0)

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