3

This is my first time developing a PHP site with JSON and also working with a online database from a Android device, my problem is that I don't know why my script returns null, I haven't send it any variables at all.

My PHP code is:

<?php

 $data = file_get_contents('php://input');
 $json = json_decode($data, true);

 var_dump($json);

?>

The question is: Which is the way to collect the JSON data that the Android Device has send to my php?

If some one asks that is the way I send the data from my Android Device to the php:

In my android app I send the JSON object this way:

    HttpResponse response =null;
    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 5000);
         HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);

    HttpPost request = new HttpPost(url);

    StringEntity se = new StringEntity(json.toString());
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
            "application/json"));
    try{
     response = httpClient.execute(request);
    }catch(SocketException sE)
    {
        throw sE;
    }

The structure of data that I'm sending from my android code is the following one:

JSONObject json = new JSONObject();
try {
        json.put("user", 0);
        json.put("status", "Confirmed");
 } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
 }
5
  • Why grab the raw input? POST it to your script and use $_POST Commented Apr 13, 2015 at 17:56
  • I am unable to figure out what url contains. As your PHP and Android code seems fine. I have worked upon alike app, so please clarify some more. Commented Apr 13, 2015 at 18:10
  • 1
    You dont seem to send the string entity anywhere. You need to add it to the http request Commented Apr 13, 2015 at 18:23
  • @AshwaniGoyal the urlis www.example.com/api.php Commented Apr 14, 2015 at 8:49
  • Okay that means the php file is api.php. Now I need to see the structure of data you are sending from your android code. Commented Apr 14, 2015 at 9:14

2 Answers 2

1

I found the solution to the problem, thanks to the comment of Mike Miller.

The problem wasn't in the PHP, was in the android app because i wasn't sending anything from the Android Device.

So the correct solution will be that one:

    HttpResponse response =null;
    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 5000);
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),3000);

    HttpPost request = new HttpPost(url);

    //Adding a header for the string
    request.setHeader("json", json.toString());

    StringEntity se = new StringEntity(json.toString());
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
            "application/json"));

    //adding the StringEntitu to the request
    request.setEntity(se);

    try{
        response = httpClient.execute(request);
    }
    catch(SocketException sE)
    {
        Log.e("SocketException", sE+"");
        throw sE;
    }

Thanks a lot every one, who have help me!

Sign up to request clarification or add additional context in comments.

Comments

0

You are using the POST method.

So your php code should be:

<?php
    $input = $_POST['some_field_name'];
    $json = json_decode($data, true);
    var_dump($json);
?>

The name of the element depends on the structure of your POSTed data.

Keep in mind that this script is going to run on the server, so you're not going to see anything happen unless your Android app is picking up the response and displaying it.

Until you have some debugging in place you might want to write the data to a file, or stub in whatever it is that you need to do with the data being posted.

Of course there are many tools from curl to various web plugins you can use to test the API. I have gotten a lot of mileage out of the Chrome app "Postman", as one suggestion.

1 Comment

Thanks for the debugging suggestion. But istill not working, the result that post man give me is null. May be is the way i send the para meters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.