Skip to content Skip to sidebar Skip to footer

Json In 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'

Solution 1:

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

Solution 2:

Here:

finalURLurl=newURL(path);
InputStreamis= url.openStream();

finalStringjString= convertStreamToString(is);
finalJSONObjectjObject=newJSONObject(jString);

finalStringjString= convertStreamToString(is);

finalJSONObjectjObject=newJSONObject(jString);

longpersonId= jObject.getLong("idpersonne");
Stringnom= jObject.getString("nom");
Stringprenom= jObject.getString("prenom");

convertStreamToString method:

private String convertStreamToString(final InputStream is)
    {

        finalBufferedReaderreader=newBufferedReader(newInputStreamReader(is));

        finalStringBuilderstringBuilder=newStringBuilder();

        Stringline=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();
    }

Solution 3:

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

Post a Comment for "Json In Android"