Hypnotize
Hypnotize

Reputation: 143

How to send JSON data as parameter from Android to Jersey RESTful Service

I am creating a login function for my Android app which will be connected to a Jersey RESTful web service. As part of the service call I will be sending a JSON object, but my problem now is that I do not know how to decrypt the JSON on the web service side. Here is my code:

Android (AsyncTask - doInBackground):

try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://X.X.X.X:8080/HearIt/services/AuthMySQL");
    post.setHeader("content-type", "application/json");

    JSONObject dato = new JSONObject();
    dato.put("email", params[0]);
    dato.put("password", Object_Cipher.init(params[1]));

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

} catch (Exception E) {
    E.printStackTrace();
}

Web Service:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL("WHAT I NEED PUT HERE") {
    return "none";
}

What do I need to do in order to be able to get the JSON data? Thanks.

Upvotes: 4

Views: 4013

Answers (2)

Perception
Perception

Reputation: 80603

I would recommend reading up on Request and Response entities in JAX-RS. You should also read Section 3.3 of the JAX-RS specification, as it covers the (very) technical aspects of how parameters in JAX-RS are handled. At the very simplest, you could simply provide a string parameter to your resource method and the incoming data (JSON) would be stored into it:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL(String json) {
    System.out.println("The JAX-RS runtime automatically stored my JSON request data: " + json);
}

Of course, this doesn't leverage the automated mapping that JAX-RS can provide. When setup properly, the runtime can actually deserialize the incoming data directly into a Java class (assuming you've created one). So, for example, given the following class:

class LoginData {
    private String email;
    private String password;

    // constructors, getters/setters
}

You can opt to marshal the request data directly into it as such:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String AuthMySQL(LoginData data) {
    System.out.println("The JAX-RS runtime automatically deserialized my JSON request data: " + data);
}

To successfully accomplish this you will need to include the jersey-json module in your application.

Upvotes: 1

fcm
fcm

Reputation: 6463

I'll try to make a generic example for you to follow.

Let's say you want to consume the following JSON:

{ "user": {
    "name": "john",
    "age": 20,
    "country": "austria"
    }
}

You would have to create the following class:

class User {
    String name;
    Integer age;
    String country;

    // getters, setters, whatevers
}

and in the webservice:

@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })   
public String consumeUserName(User user) {
    return user.getName();
}

Upvotes: 0

Related Questions