String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.billboard.billList&format=json&type=2&offset=0&size=50";
//1.消息请求队列 RequestQueue
queue= Volley.newRequestQueue(this);
// 2. StringRequest
SuccessListener success = new SuccessListener();
ErrorListener error = new ErrorListener();
StringRequest re=new StringRequest(url, success, error){
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String str = null;
try {
str = new String(response.data, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Response.success(str, HttpHeaderParser.parseCacheHeaders(response));
}
};
// 3. queue.add(request)
queue.add(re);
}
可以直接在url的数据,通过listview解析显示出来,用到消息请求队列requstqueue还需要建立一个 QuaryResult的类,
class SuccessListener implements Listener<String> {
public void onResponse(String response) {
Gson gson = new Gson();
QuaryResult result = gson.fromJson(response, QuaryResult.class);
musics=result.getSong_list();
//更新Adapter listView
setAdapter();
}
}
public void setAdapter() {
adapter = new MusicAdapter(musics,this);
listview.setAdapter(adapter);
}
class ErrorListener implements com.android.volley.Response.ErrorListener {
public void onErrorResponse(VolleyError error) {
// .....
}
}