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

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

Android (AsyncTask - doInBackground):

try { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://XXXX: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 to get JSON data? Thanks.

+4
source share
2 answers

I would recommend reading in Request and Response Objects 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 are handled in JAX-RS. In the simplest case, you can simply provide a string parameter for your resource method, and input data (JSON) will be saved in 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 does not affect the automatic mapping that JAX-RS can provide. When properly configured, the runtime can actually deserialize the incoming data directly into the Java class (provided that you created it). So, for example, given the following class:

 class LoginData { private String email; private String password; // constructors, getters/setters } 

You can choose to marshal the request data directly in 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 need to include jersey-json in your application.

+1
source

I will try to make a general example for you.

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

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

You will need to create the following class:

 class User { String name; Integer age; String country; // getters, setters, whatevers } 

and in the web service:

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

Source: https://habr.com/ru/post/1481225/


All Articles