-1

Currently I am only able to actually read the php file that contains a json format.

What I want to do is read an echo statement (in json format) in replace of reading the actual file.

This is my android code so far to request a php file that contains a json format for me to read:

// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent(); // This is storing the contents from the php file

Thanks in advance

This is the php file:

{
"user": [
{
"id": "001",
"name": "Raj Amal",
"email": "raj.amalw@gmail.com"
}
]
}

I want to do this:


<?php
    echo '{
    "user": [
    {
    "id": "001",
    "name": "Raj Amal",
    "email": "raj.amalw@gmail.com"
    }
    ]
    }';
    ?>
scooter
  • 13
  • 1
  • 9
  • It isn't very clear to me what you are asking - what is the response you are getting, and why can't you read it? Do you need to parse the JSON or something? – ajshort Dec 08 '15 at 02:12

2 Answers2

1

Try as below,

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);

Now the response from the server will be found in the httpResponse. You can convert that to string like below.

String myResponseString = EntityUtils.toString(httpResponse.getEntity());

Log the string myResponseString to see you response from the server

codePG
  • 1,754
  • 1
  • 12
  • 19
0

I think your issue is with server configuration. meaning - the plain php (which is json) being recognized by your server as json and returns with appropriate headers. while your echo returns as plain text without the header which by HttpEntity (which is deprecated!).

On the client side the answer above with HttpClient is the way to go.

On the server side:

<?PHP
$data = /** put your content here! **/;
header('Content-Type: application/json');
echo json_encode($data); 

https://stackoverflow.com/a/4064468/324482

Community
  • 1
  • 1
Rock_Artist
  • 555
  • 8
  • 14