how to read Json data sent by a php file in android
here is the php code
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
echo $data_string;
here is the code to read data android json
InputStream instream = entity.getContent();
// String result = "";
Log.i("--------etat---------r","-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-");
String result = this.convertStreamToString(instream);
Log.i("--------etat---------r","-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-");
here is the method to analyze the InputStream object
public String convertStreamToString(InputStream is) {
convertStreamToString " );
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
but here is the code that is displayed
11-15 10:46:58.744: I/Read from server(526): 11-15 10:46:58.744: I/Read from server(526): ( ! ) SCREAM: Error suppression ignored for 11-15 10:46:58.744: I/Read from server(526): ( ! ) Notice: Undefined index: json in C:\wamp\www\younes_project\younesservices.php on line 4 11-15 10:46:58.744: I/Read from server(526): Call Stack 11-15 10:46:58.744: I/Read from server(526): #TimeMemoryFunctionLocation 11-15 10:46:58.744: I/Read from server(526): 10.0006142200{main}( )..\younesservices.php:0 11-15 10:46:58.744: I/Read from server(526): 11-15 10:46:58.744: I/Read from server(526): {"name":"Hagrid","age":"36"}
but i have read only variable
this :{"name":"Hagrid","age":"36"}