user6457240
user6457240

Reputation:

Android HTTP PUT not sending JSON request to server resulting in HTTP 405 Method not allowed

Android HTTP PUT not sending JSON request to server resulting in HTTP 405 Method not allowed.

Below is my async task background code

HttpClient httpclient = new DefaultHttpClient();
            HttpPut httpPut = new HttpPut("URL");

            String jsonresponse = "";
            try {
                StringEntity se = new StringEntity(gson.toJson(resultPojo).toString());
                se.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));

                httpPut.setEntity(se);
                httpPut.setHeader("Accept", "application/json");
                httpPut.setHeader("Content-type", "application/json");
                HttpResponse response = httpclient.execute(httpPut);
                HttpEntity httpEntity = response.getEntity();
                jsonresponse = EntityUtils.toString(httpEntity);
                System.out.println("res .... "+jsonresponse);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

serverside code :

@POST
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response submitUserResponse(@PathParam("userId") int userId,
            @PathParam("id") int id, List<ResultPojo> responses) {
            try {
            //core logic goes here
                        return Response.status(Response.Status.CREATED).build();
} catch (Exception e) {
            e.printStackTrace();
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
        }
            }

Upvotes: 1

Views: 322

Answers (1)

Remario
Remario

Reputation: 3863

Alright just like what was discussed it is most likely a mismatch different HTTP methods, in this case A Put and a post, whenever you ever encounter that HTTP code(405) do perform a validation on the methods you used, it happens.405 errors often arise with the POST method. You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form.A request method is not supported for the requested resource; for example, a GET request on a form that requires data to be presented via POST, or a PUT request on a read-only resource.

Upvotes: 1

Related Questions