Im writing an android app that connects to my own Jersey rest client. HTTP get commands work fine, but im having trouble with my POSTs where im trying to send something to the server. I get a 405 sent back, so it seems like the server cannot match the request up with the resource methods. Any thoughts? Test code below…
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 shows the following
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/UNAVAILABLE (java 1.4)
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="Testpart1"
<testxml></testxml>
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7
Content-Disposition: form-data; name="image1"
imagedata1
--jju2JFDOlzJ4LQo7YkrJYLuwDUHmB5b7--
Thanks
Mark
You are sending HTTP POST, but on the server side you declare a handler for HTTP PUT only. So, it’s not able to match POST to any method hence 405. Change the annotation on your resource method from @PUT to @POST or send HTTP PUT instead of POST by the client.