user1137313
user1137313

Reputation: 2410

Android, POST a JSON array , php debugging

I managed to build a JSON array and I have a onClick event where I send this JSONArray through POST towards a php file that will process the array.

Right now I have no idea if the array arrives at the destination or not, if the array is interpreted correctly by the PHP or not. The code for my POST call (Android) is:

Button bsave = (Button) findViewById(R.id.button3);
    View.OnClickListener eventHandlerx = new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            JSONObject j1;
            JSONArray jsonarray=new JSONArray();
            ListView lst = (ListView) findViewById(R.id.mylistview);
            int count = lst.getCount();
            for (int i = 0; i < count; i++) 
            {
                ViewGroup row = (ViewGroup) lst.getChildAt(i);
                TextView tvId = (TextView) row.findViewById(R.id.fID);
                TextView tvNume = (TextView) row.findViewById(R.id.fTitlu);
                TextView tvUM = (TextView) row.findViewById(R.id.fUM);
                TextView tvPU = (TextView) row.findViewById(R.id.fPU);
                TextView tvCant = (TextView) row.findViewById(R.id.fCant);

                jsonarray.put("Item");
                j1 = new JSONObject();
                try {
                    j1.put("idx", newid);
                    j1.put("id", tvId.getText());
                    j1.put("nume", tvNume.getText());
                    j1.put("um", tvUM.getText());
                    j1.put("pu", tvPU.getText());
                    j1.put("cant", tvCant.getText());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                jsonarray.put(j1);
            }               
            Log.d("JSON array","Array to send:"+jsonarray.toString());
            sendJson( urlx, jsonarray);
        }

        private void sendJson(final String urlx, final JSONArray jsonarray) {
                Thread t = new Thread() {
                    public void run() {
                        Looper.prepare(); //For Preparing Message Pool for the child Thread
                        HttpClient client = new DefaultHttpClient();
                        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                        HttpResponse response;
                        try {
                            HttpPost post = new HttpPost(urlx);
                            StringEntity se = new StringEntity( jsonarray.toString());  
                            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                            post.setEntity(se);
                            response = client.execute(post);

                            /*Checking response */
                            if(response!=null){
                                HttpEntity entity = response.getEntity();
                                if (entity != null) {
                                    Log.e("POST response",EntityUtils.toString(entity));
                                }else{
                                    Log.e("POST response","NULL "+EntityUtils.toString(entity));
                                }
                            }else{
                                Log.e("POST response","NULL");
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                            Log.e("postJSON","Error at POST with json:"+e.toString());
                        }
                        Looper.loop(); //Loop in the message queue
                    }
                };
                t.start();      
            }   
    };        
    bsave.setOnClickListener(eventHandlerx);        

How do I check if the array was successfully POSTED to php? I assume I could do it in a php file... But how do I see the result? No matter what I do, the Log shows me:

org.apache.http.conn.EofSensorInputStream@somehexa

So I am stuck. Please give me some solutions. What should a php file look like in order to return the POSTED value, and what should my Android code look like in order to interpret the result (just display it somewhere like Log for debugging purposes)

The php I use is:

<?php
ob_start();
var_dump($_POST);
die();
$json = file_get_contents('php://input');
$obj = json_decode($json);
print $obj;
result = $obj;

$page = ob_get_contents();
ob_end_flush();
$fp = fopen("output.txt","w");
fwrite($fp,$page);
fclose($fp);
?>

However, the file does not show up... and Eclipse does not give me anything in Log...

Thank you

Upvotes: 0

Views: 2699

Answers (1)

pajaja
pajaja

Reputation: 2212

If this is just for testing the easiest way to see the data is to do var_dump($_POST); inside PHP file you are submitting the form to, and log the response to some file on web server or to console in your android app. You can put die(); after var_dump if you want to break the execution of the rest of the code while you are testing.

edit:

Since you are using php://input stream and already have some code you can just use var_dump($json) or just echo it back you your app instead of what i suggested earlier. This will return the raw data you posted but you will need some kind of code in your app to display the HttpResponse. You can try something like this:

HttpEntity entity = response.getEntity();
String phpResponse = EntityUtils.toString(entity);

and then log the phpResponse variable.

Also, your file is not generated since everything after die(); is not executed, you can remove it and check again if the file is there.

Upvotes: 1

Related Questions