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());
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
java json android rest web-services
Mududir shahzad
source share