I want to consume a json web service ,
this is the url : http://10.0.2.2:8888/RESULT.JSON
{
"idpersonne":1,
"nom":"AAA",
"prenom":"AAA",
}
how I can retrieve the object "person" using android ?
I want to consume a json web service ,
this is the url : http://10.0.2.2:8888/RESULT.JSON
{
"idpersonne":1,
"nom":"AAA",
"prenom":"AAA",
}
how I can retrieve the object "person" using android ?
Behold the JSONObject
, which reads String
s and will let you pull pieces out by name.
Here:
final URL url = new URL(path);
InputStream is = url.openStream();
final String jString = convertStreamToString(is);
final JSONObject jObject = new JSONObject(jString);
final String jString = convertStreamToString(is);
final JSONObject jObject = new JSONObject(jString);
long personId = jObject.getLong("idpersonne");
String nom = jObject.getString("nom");
String prenom = jObject.getString("prenom");
convertStreamToString method:
private String convertStreamToString(final InputStream is)
{
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder stringBuilder = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
} catch (final IOException e)
{
L.e(e.getMessage());
} finally
{
try
{
is.close();
} catch (final IOException e)
{
L.e(e.getMessage());
}
}
return stringBuilder.toString();
}