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;
}