我尝试做的是来自服务器的fetch
艺术家值,以便我可以将数据与基本网址相结合。
我已将InputStream
转换为String
,但我认为将json
字符串转换为HashMap
时遇到了一些问题。我已经坚持了这个问题2周...... :(
以下是服务器的结果。 (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"}]}
编辑器格式化的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;
}
答案 0 :(得分:0)
你知道json的密钥,所以你可以使用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);
// ...
和MusicInfo类也很简单。
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;
}
}
然后你会得到
I/System.out﹕ artists: Gummy
I/System.out﹕ artists: Beenzino
I/System.out﹕ artists: BrunoMars
I/System.out﹕ artists: BrunoMars