0

I'm trying to POST data to PHP using Android. But getting Null on receiving in PHP. Here is my Code...

Android Code:

try
{
    JSONStringer().object()

    JSONObject data = new JSONObject();
    data.put("phone", "11");
    data.put("pwd", "aa");

    HttpPost request = new HttpPost("http://www.cslindia.org/viz/test.php");
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");

    StringEntity entity = new StringEntity(data.toString());
    request.setEntity(entity);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);

    HttpEntity httpEntity =response.getEntity();
    String json = EntityUtils.toString(httpEntity);
    Log.e("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",json);
}

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

PHP Code:

$json = file_get_content('php://input');
print_r("1".$json); //For Testing Purpose
$jsonObj = json_decode($json, true);
print_r("2".$jsonObj); //For Testing Purpose
$mphone=$jsonObj['phone'];
$mpwd=$jsonObj['pwd'];

But, $json, $jsonObj both have null value. What may be the solution please help me out...

Sir l33tname
  • 4,026
  • 6
  • 38
  • 49
  • check this link http://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using-php-slim-and-mysql-day-12-2/ – rajahsekar May 12 '14 at 12:38
  • It was asked many times. For example: http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android – 3mpty May 12 '14 at 12:39
  • Maybe this post helps http://stackoverflow.com/questions/14983521/send-json-from-java-to-php-through-post – michoprogrammer May 12 '14 at 12:40

0 Answers0