0

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 ?

user1187282
  • 1,137
  • 4
  • 14
  • 23

3 Answers3

1

Behold the JSONObject, which reads Strings and will let you pull pieces out by name.

Argyle
  • 3,324
  • 25
  • 44
0

I would suggest you to use Gson to parse json strings. Read this tutorial

waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

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();
    }
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68