I am new in Android. I am trying to make an RPC to fetch data from PHP and get in JSON format. Everything goes fine but I am not getting any data as response. Bellow is my android code
Android Code
try{
HttpClient httpClient = new DefaultHttpClient();
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("id", 0);
jsonRequest.put("method", "getData");
HttpEntity entity = new StringEntity(jsonRequest.toString());
HttpPost request = new HttpPost("http://121.247.130.30:8080/test/TestJava.php");
request.setEntity(entity);
HttpResponse response = httpClient.execute(request);
String temp = EntityUtils.toString(response.getEntity());
Log.e("Class====","Error:"+temp);
}
catch(Exception e){
Log.e("Class====","Error:"+e.getMessage());
}
PHP Code
<?php
class TestJava
{
public function getData()
{
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
}
}
?>
Please let me know if anything I did here
I think you misused entity element. Check what I have in my code:
StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK ){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); response.getEntity().consumeContent(); out.close(); return out.toString(); }