Asim
Asim

Reputation: 7114

Reading JSON data using GSON

I spent the past 4 hours looking at various answers and other resources but I simply can't wrap my head around JSON parsing. I need some help.

Here is the JSON string:

{
  "success": true,
  "categories": [
    {
      "category_id": "20",
      "parent_id": "0",
      "name": "Desktops",
      "image": "***",
      "href": "***",
      "categories": null
    },
    {
      "category_id": "25",
      "parent_id": "0",
      "name": "Components",
      "image": "***",
      "href": "***",
      "categories": null
    },
    {
      "category_id": "34",
      "parent_id": "0",
      "name": "MP3 Players",
      "image": "***",
      "href": "***",
      "categories": null
    }
  ]
}

Here is my Data class:

public class Data
{
    String success;
    List<Category> categories;

    // Various get/set functions and a toString override

    public class Category
    {
        String category_id;
        String name;
        String image;

        // Various get/set functions
    }
}

Here is where I'm trying to read this:

private class GetJson extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            String results = "Fail";
            URL url = null;
            try
            {
                url = new URL("***");
            }

            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }

            URLConnection ucon = null;

            try
            {
                ucon = url.openConnection();
                InputStream is = ucon.getInputStream();
                BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(is));
                String line = "";
                String result = "";
                while((line = bufferedReader.readLine()) != null)
                {
                    result += line;
                }

                Data data = new Gson().fromJson(result, Data.class);

                result = data.toString();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return results;
        }

        protected void onPostExecute(String result)
        {
            Toast.makeText(con, result, Toast.LENGTH_SHORT);
        }
    }

I'm not getting anything on the stacktrace at all. I checked ADB several times. Everything seems to be working but I get no Toast or error message.

What am I doing wrong?

Upvotes: 1

Views: 98

Answers (2)

SorryForMyEnglish
SorryForMyEnglish

Reputation: 1181

you forgot to show your Toast

try this

Toast.makeText(con, result, Toast.LENGTH_SHORT).show();

lol

and further

    Data data = new Gson().fromJson(result, Data.class);

    result = data.toString();
    return result; // need return this

otherwise will always get "Fail"

Upvotes: 2

koutuk
koutuk

Reputation: 832

//First generate getter setter of data class variables. 

 TypeToken<Data> tokenPoint1 = new TypeToken<Data>() {};

//SYSO result string here for Confirmation

  Gson gson = new Gson();

 Data dataobj= gson.fromJson(result, tokenPoint1.getType());

 // syso dataobj.getname();

//Hope this will work for you

Upvotes: 0

Related Questions