Transmit and get JSON object from Jersey Resftul web service from android

Scenario: pass username and password in json object for a quiet web service and get json object in return. Yes, I know, itโ€™s easy, but I canโ€™t get it to work.

I tried to do this a few days later. So far I have tried this:

My calm webservice code

@POST @Path("/testJson") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public JSONObject testJson(JSONObject inputJsonObj) throws Exception { JSONObject myjson = new JSONObject(); if(inputJsonObj != null){ System.out.println("================================="); System.out.println("JSON object = " + inputJsonObj.toString()); System.out.println("================================="); } else{ System.out.println("JSON is NULL"); } myjson.put("success", "1"); System.out.println(myjson.toString()); // return "string returned"; return myjson; } 

And inside my android, code

 // POST request to <service>/SaveVehicle HttpPost request = new HttpPost(myURL); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); request.setHeader("user-agent", "Yoda"); try { // Build JSON string // JSONStringer vehicle = new JSONStringer().object() // .key("getItInputTO").object().key("zipCode").value("90505") // .key("financingOption").value("B").key("make") // .value("Scion").key("baseAmountFinanced").value("12000") // .key("modelYear").value("2010").key("trimCode") // .value("6221").key("totalMSRP").value("15000") // .key("aprRate").value("").endObject().endObject(); JSONObject myjson = new JSONObject(); myjson.put("1", "first"); myjson.put("2", "second"); StringEntity entity = new StringEntity(myjson.toString()); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json; charset=utf-8")); request.setEntity(entity); // Send request to WCF service DefaultHttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // HttpResponse response = httpClient.execute(request, localContext); HttpResponse response = httpClient.execute(request); resCode = response.getStatusLine().getStatusCode(); Toast.makeText(getApplicationContext(), response.getStatusLine().getStatusCode() + "", Toast.LENGTH_LONG).show(); if (resCode == 200) { Toast.makeText(getApplicationContext(), response.getStatusLine().getStatusCode() + "", Toast.LENGTH_LONG).show(); HttpEntity entity2 = (HttpEntity) response.getEntity().getContent(); String text = getASCIIContentFromEntity(entity); if(text!=null){ JSONObject obj = new JSONObject(text); lblMsg.setText("Successful!"); } // BufferedReader in = new BufferedReader(new // InputStreamReader(response.getEntity().getContent())); // // // String line = ""; // StringBuffer returnFromServer = new StringBuffer(); // // while ((line = in.readLine()) != null) { // returnFromServer.append(line); // } // // Toast what we got from server // Toast.makeText(getApplicationContext(), // returnFromServer.toString(), Toast.LENGTH_LONG).show(); // // if (entity != null) { // entity.consumeContent(); // } } } catch (Exception e) { // TODO: handle exception } 

The commented sections show previous attempts.

The result that I get on the server console

 ================================= JSON object = {} ================================= {"success":"1"} 

My server receiver of the json object is not populating, I do not know why.

Note:

  • I have INTERNET and many other permissions in the Android manifest.
  • My web service is up and running.
  • I have all the necessary banks, for example, jersey, json, etc.
  • I use Tomcat 7 for a calm webservice

I would really appreciate any help. Thanks

+7
java json android rest web-services
source share
2 answers

I have the same problem as you. I do not know why, but the temporary solution that I use creates a class to handle these parameters.

This means using Jackson to transform Json Object <=> "Your Class"

See this tutorial for more information: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

=====================================

And I just found this topic, it may be more useful than the top solution: The Jersey POST method gets null values โ€‹โ€‹as parameters

0
source share

It may be too late to answer, but here:

  • First change the POST to GET
  • Change @Path
  • And add @PathParam to your code

The changes are as follows:

 @GET @Path("/testJson/{inputJsonObj}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public JSONObject testJson(@PathParam("inputJsonObj") JSONObject inputJsonObj) throws Exception { ... 

Yes, you must indicate when your resource accepts the parameter.

Also note that once you change the request method, you need to change the way android: inteast HttpPost use HttpGet ...

-one
source share

All Articles