0

What I'm trying to do is fetch artists values from the server, so that I can combine the data with the base url. I've done converting InputStream to String but I think I got some problems with converting json String to HashMap. I'm stuck with this problem for 2 weeks... :(

Here is the result from the server. (String inputStr)

{"musicInfo":      [{"music_id":"4","artists":"Gummy","file_name":"Thinkaboutme","jacket_thumbnail":"thThink","jacket_name":"oriThink","is_on_server":"1","is_liked":"0","created":"2014-08-11 20:00:17"},{"music_id":"3","artists":"Beenzino","file_name":"Aquaman","jacket_thumbnail":"thAquaman","jacket_name":"oriAquaman","is_on_server":"1","is_liked":"0","created":"2014-08-11 19:59:31"},{"music_id":"2","artists":"BrunoMars","file_name":"Justthewayyouare","jacket_thumbnail":"thJustTheWayYouAre","jacket_name":"oriJustTheWayYouAre","is_on_server":"1","is_liked":"0","created":"2014-08-11 19:58:21"},{"music_id":"1","artists":"BrunoMars","file_name":"Grenade","jacket_thumbnail":"thGrenade","jacket_name":"oriGrenade","is_on_server":"1","is_liked":"0","created":"2014-08-11 19:56:51"}]}

Editor's formatted JSON:

{
    "musicInfo": 
    [
        {
            "music_id": "4", 
            "artists": "Gummy", 
            "file_name": "Thinkaboutme", 
            "jacket_thumbnail": "thThink", 
            "jacket_name": "oriThink", 
            "is_on_server": "1", 
            "is_liked": "0", 
            "created": "2014-08-11 20:00:17"
        }, 
        {
            "music_id": "3", 
            "artists": "Beenzino", 
            "file_name": "Aquaman", 
            "jacket_thumbnail": "thAquaman", 
            "jacket_name": "oriAquaman", 
            "is_on_server": "1", 
            "is_liked": "0", 
            "created": "2014-08-11 19:59:31"
        }, 
        {
            "music_id": "2", 
            "artists": "BrunoMars", 
            "file_name": "Justthewayyouare", 
            "jacket_thumbnail": "thJustTheWayYouAre", 
            "jacket_name": "oriJustTheWayYouAre", 
            "is_on_server": "1", 
            "is_liked": "0", 
            "created": "2014-08-11 19:58:21"
        }, 
        {
            "music_id": "1", 
            "artists": "BrunoMars", 
            "file_name": "Grenade", 
            "jacket_thumbnail": "thGrenade", 
            "jacket_name": "oriGrenade", 
            "is_on_server": "1", 
            "is_liked": "0", 
            "created": "2014-08-11 19:56:51"
        }
    ]
}

MainActivity.java:

private String getMusicInfo(String url) {

    InputStream inputStream = null;
    String inputStr = "";
    HashMap<String, String> map = new HashMap<String, String>();

    try {
        URL getMusicUrl = new URL(url);
        conn = (HttpURLConnection) getMusicUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        Log.d(TAGCP, "MADE POST REQUEST TO THE GIVEN URL");

        inputStream = conn.getInputStream();


        if (inputStream != null) {
            inputStr = Util.convertInputStreamToString(inputStream);
            Log.i(TAGCS, inputStr);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(inputStr);
            String artistsName = (String) jsonObject.get("artists");
            // Debug: artistsName == null... what is happening!!?
            System.out.println(artistsName);

        } else {
            inputStr = "Did not work!";
            Log.d(TAGRR, inputStr);
        }
    } catch(Exception e) {
        Log.i("InputStream", e.getLocalizedMessage());
    }

    return inputStr;

} 
  • Please specify what the hash map should contain, what are the intended keys, what are the intended values? At the moment it will just have one entry with key `musicInfo` – Henry Aug 15 '14 at 05:50
  • Format your code yourself before posting here next time. – Unihedron Aug 15 '14 at 05:55
  • I'm so sorry I haven't think about that...:( –  Aug 15 '14 at 05:58
  • hashmap should contain music_id, artists,file_name, jacket_thumbnail.. –  Aug 15 '14 at 05:58
  • Is your `JSON` from `org.json` or `org.json.simple`? – Unihedron Aug 15 '14 at 05:59
  • I tried both of them.. But they were not working for me.. Maybe I used them inappropriate way. –  Aug 15 '14 at 06:08
  • "hashmap should contain music_id, artists,file_name, ..." but there are four entries, you want the data of which one? – Henry Aug 15 '14 at 06:12

1 Answers1

0

You know keys the json have, so you can write quite simple by using GSON.

// ...

String json = "YOUR JSON";
MusicInfo list = new Gson().fromJson(json, MusicInfo.class);
for(MusicInfo.Info info: list.musicInfo) {
System.out.println("artists: " + info.artists);

// ...

and MusicInfo class is also simple.

import java.util.List;

public class MusicInfo {

    public List<Info> musicInfo;

    public static class Info {
        public String artists;
        public String created;
        public String file_name;
        public int is_liked;
        public int is_on_server;
        public String jacket_name;
        public String jacket_thumbnail;
        public long music_id;
    }
}

then you will get

I/System.out﹕ artists: Gummy
I/System.out﹕ artists: Beenzino
I/System.out﹕ artists: BrunoMars
I/System.out﹕ artists: BrunoMars
ytRino
  • 1,450
  • 15
  • 28
  • I have one more question! is there any other way to put string json to hashmap? so that I can use it anytime I want. –  Aug 15 '14 at 06:29
  • I found this answer http://stackoverflow.com/a/15943171/1726166 but I have not tried it yet. It seems convert all values to String. – ytRino Aug 15 '14 at 06:34