0
votes

I am trying to call a jersey restful web service from android. My android code is

Client code:

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://X.X.X.X:8080/RestfulService/rest/post");
post.setHeader("content-type", "application/json");

JSONObject dato = new JSONObject();
dato.put("email", email);
dato.put("password", password);

StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String rs = EntityUtils.toString(resp.getEntity());
return rs

Webservice code

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL(JSONObject json) {

String password = (String) json.get("password");
String email = (String) json.get("email");

*I am using the string values to get the result from the database*

}

The error i get is something like com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, class org.json.JSONObject.... and MIME media type, application/json was not found.

Your help is much appreciated

2
Can you list the library files that attached to your service project ? - Madhusudan Joshi
@Joshi i have included all the jersey 1.18 jar files - Mahi
You were getting a 415 Unsupported Media Type, no? I've struggled with this too. Tomcat, or whatever's on your back end, doesn't know anything but String. If you change JSONObject to String, you'll find you actually get inside method AuthMySQL() with json just as string, but then you've got either to make that parsable so you can use the code you've written or set up something that Jersey can use to translate the JSON string coming in into a JSONObject. This is where I'm stuck. (Don't know if this helps or is too late to help.) - Russ Bateman

2 Answers

0
votes

This occurs when you don't have the correct libraries included to properly map the json to a POJO or there is not an appropriate POJO for the input.

Look at adding the jersey-json maven dependency to your project

0
votes

If you don't want to add a library, and just want to get at the parsed JSON (i.e. without mapping to a POJO), then you can just implement a basic MessageBodyReader, e.g:

public class JSONObjectMessageBodyReader implements MessageBodyReader<JSONObject> {
    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return type == JSONObject.class && mediaType.equals(MediaType.APPLICATION_JSON_TYPE);
    }

    @Override
    public JSONObject readFrom(Class<JSONObject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
        try {
            // Using Apache Commons IO:
            String body = IOUtils.toString(entityStream, "UTF-8");
            return new JSONObject(body);
        } catch(JSONException e) {
            throw new BadRequestException("Invalid JSON", e);
        }
    }
}

Then in your webservice code:

@POST
public Response doSomething(JSONObject body) {
   ...
}