Abhishek Dhiman
Abhishek Dhiman

Reputation: 1651

Getting JSON values from URL in android

In my application i am trying to retrieve the values from a JSON URL. In the URL there is a point where I am not able to parser the values.. I have tried alot to find the solution but I haven't got any..

In the URL the values that I want to parse is the values of tag_users...

I am attaching the code where I am trying to parser the values so, please help me in finding the solution...

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

String url = "http://flutterr.pnf-sites.info/api/messageshow/2";
        JsonParser jParser = new JsonParser();
        JSONObject json = jParser.getJSONfromUrl(url);
        try
        {
            JSONArray messageshow = json.getJSONArray("messageshow");
            userList.clear();
            for(int i=0; i<messageshow.length();i++)
            {
                JSONObject msg = messageshow.getJSONObject(i);
                message_id = msg.getString("message_id");
                message = msg.getString("message");
                message_pic = msg.getString("message_pic");
                uid = msg.getString("uid");
                created = msg.getString("created");
                username = msg.getString("username");
                first_name = msg.getString("first_name");
                last_name = msg.getString("last_name");
                profile_pic = msg.getString("profile_pic");
                total_likes = msg.getString("total_likes");
                JSONObject tag_user = msg.getJSONObject("tag_user");
                message = tag_user.getJSONObject("tags").getString("message");
                if(message != "false")
                    tag_uid = tag_user.getJSONObject("tags").getString("tag_uid");
                Log.v("uid", tag_uid);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("message_id", message_id);
                map.put("message", message);
                map.put("message_pic", message_pic);
                map.put("uid", uid);
                map.put("created", created);
                map.put("username", username);
                map.put("first_name", first_name);
                map.put("last_name", last_name);
                map.put("profile_pic", profile_pic);
                map.put("total_likes", total_likes);
                userList.add(map);
            }
        }
        catch(JSONException e)
        {
            Log.e("Error", e.toString());
        }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Please help me in letting me know that how can I parser the values of tag_uid, tag_uname, tag_fname and tag_lname...

Upvotes: 0

Views: 312

Answers (2)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

try

if(!message.equals("false"))

instead of

if(message != "false")

One more thing check your server side code

in tag_user there is Json Object with name tags in case of zero tags. And it is (tags) is a JsonArray in case of results.

EDIT

if you Json will look like this

   "tag_user": {
  "tags": [
    {
      "tag_uid": "1",
      "tag_fname": "abhishek",
      "tag_lname": "dhiman",
      "tag_uname": "abhishek"
    }
  ]"message": "true" //here true or false to check "tags" array
}

and then in code you can get like

message = tag_user.getJSONObject("message");

                    if(!message.equals("false"))
                    tag_uid = tag_user.getJSONArray("tags").getJSONObject(0).getString("tag_uid");

//will return "tag_uid" of first tag`

Upvotes: 2

avianey
avianey

Reputation: 5813

tag_user is a JSON object that contains an array :

"tag_user":{"tags":[{"tag_uid":"1","tag_fname":"abhishek","tag_lname":"dhiman","tag_uname":"abhishek"},{"tag_uid":"4","tag_fname":"tttttt","tag_lname":"tttttt","tag_uname":"tttttt"},{"tag_uid":"3","tag_fname":"qqqqqq","tag_lname":"qqqqqq","tag_uname":"qqqqqq"},{"tag_uid":"13","tag_fname":"Firstname","tag_lname":"Lastname","tag_uname":"Username4"}]}

you should try

getJSONArray("tags")

on tag_user object

If you're familiar with Sax parsing try JsonReader from the Gson project : https://sites.google.com/site/gson/streaming

Upvotes: 0

Related Questions