-4

I am massively stuck with converting a PHP server request into an equivalent Java Request. This is the code that contains the JSON object that I need to replicate in JAVA and send from an Android device:

$(".unableprocess").click(function() {
            if (!confirm("Confirm not able to process...!")) {
                return false;
            } else {
                var item_id = $(this).attr('data-id');
                var table_id = $(this).attr('table-id');
                var data = {
                    BookOrders: {
                        item_id: item_id,
                        table_id: table_id
                    }
                };
                $.ajax({
                    url:  //MY URL HERE ,
                    type: "POST",
                    data: data,
                    success: function(evt, responseText) {
                        location.reload();

                    }
                });
            }
});

And here is my Java class that attempts to perform the same functionality. The class extends AsyncTask and all network interactions occur in the doInBackground() method. Here is my code:

@Override
protected Boolean doInBackground(String... arg0) {

   try{


        HashMap<String, String> hashMap = new HashMap<String,String>();

        JSONObject jsonObject = new JSONObject();
        int statusCode;

        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(tableMateCannotProcessURL);

        // JSON object creation begins here:

        jsonObject.accumulate("item_id",this.itemId);
        jsonObject.accumulate("table_id",this.tableId);

        JSONObject jObject = new JSONObject();

        jObject.accumulate("BookOrders", jsonObject);

        // JSON object ends here 

        Log.v("ATOMIC BLAST",jObject.toString());

        String json = jObject.toString();
        StringEntity se = new StringEntity(json);


        httpPost.setEntity(se);

        HttpResponse response = client.execute(httpPost);
        statusCode = response.getStatusLine().getStatusCode();
        Integer statusCodeInt = new Integer(statusCode);
        Log.v("HTTPResponse",statusCodeInt.toString());

        String result= "";
        StringBuilder builder = new StringBuilder();

        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) { 
                builder.append(line); 
            }

            result = builder.toString();

        } 

        else { 
            Log.e("==>", "Failed to download file"); 
        }

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

    return null;
}

The JSON object that I created looks like this after printing it out to the console:

{"BookOrders":{"table_id":"1","item_id":"2"}}

After POSTing this object to the server I do not get the expected response. What is the proper method for converting the JSON object into an equivalent JSON object in JAVA? Any guidance, direction or a solution would be most appreciated.

user1841702
  • 2,683
  • 6
  • 35
  • 53

2 Answers2

0

Update php to version 5.4 helped me. In this version json_encode($x, JSON_PRETTY_PRINT) works just as needed.

user2615287
  • 91
  • 1
  • 2
0

Your JSON seems to be correct but it's an Object in an Object.

 JSONObject json = new JSONObject(yourdata);
 JSONObject jsonTable = new JSONObject(json.getString("BookOrders"));

 Log.d("JsonDebug", "json:" + jsonTable.toString());

If you are not sure if you have a JSONObject or an Array you can validate it by using

String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
//you have an object
else if (json instanceof JSONArray)
//you have an array
Emanuel
  • 8,027
  • 2
  • 37
  • 56