1

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?

3
  • First of all success is of type boolean Commented Jan 21, 2015 at 12:22
  • use jackson (github.com/FasterXML/jackson) Commented Jan 21, 2015 at 12:22
  • You forgot to close the input stream.. Commented Jan 21, 2015 at 12:28

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. And I have to admit, this was the dumbest mistake I have ever posted on stackoverflow. I was kinda sleepy and I guess this is what happens lol. Thanks! Marked.
you are welcome. I myself often make such mistakes :-)
0
//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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.