我正在尝试解析json数组,但是JsonArrayRequest OnResponse方法没有调用。所以我的专辑列表返回null。我在互联网上搜索,但我无法解决问题。
这是我的代码:
public class ListFragment extends Fragment {
View rootView;
ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.listfragment, container, false);
listView = (ListView)rootView.findViewById(R.id.listView);
new ListAsync(getActivity(), listView).execute();
return rootView;
}
private class ListAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog_dunya;
String urlAlbum = "http://www.jsonplaceholder.typicode.com/albums";
ListView mListView;
Activity mContex;
int userid, id;
String title;
MyListAdapter myListAdapter;
List<Album> albums;
Album album;
ListAsync(Activity contex, ListView gview)
{
this.mListView=gview;
this.mContex=contex;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog_dunya = new ProgressDialog(mContex);
progressDialog_dunya.setTitle("Yükleniyor...");
progressDialog_dunya.setMessage("Yükleniyor...");
progressDialog_dunya.setIndeterminate(false);
progressDialog_dunya.show();
}
@Override
protected Void doInBackground(Void... params) {
Log.d("RESPONSE ALBUM", "hey" + urlAlbum);
albums = getJsonAlbum();
Log.d("RESPONSE ALBUM", "hey al" + albums);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
myListAdapter = new MyListAdapter(mContex, albums);
mListView.setAdapter(myListAdapter);
progressDialog_dunya.dismiss();
}
List<Album> getJsonAlbum(){
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
Log.d("RESPONSE ALBUM", "IN GETJSON");
JsonArrayRequest jsonObjReq = new JsonArrayRequest(
urlAlbum, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("RESPONSE ALBUM", "response"+response.toString());
try {
albums = new ArrayList<Album>();
for (int i = 0; i< response.length();i++) {
album = new Album();
JSONObject jsonObject = (JSONObject) response.get(i);
userid = jsonObject.getInt("userId");
id = jsonObject.getInt("id");
title = jsonObject.getString("title");
album.setId(id);
album.setTitle(title);
album.setUserId(userid);
albums.add(album);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("volley", "error");
}
});
requestQueue.add(jsonObjReq);
return albums;
}
}
}
这是我的日志:
D/RESPONSE ALBUM: heywww.jsonplaceholder.typicode.com/albums
D/RESPONSE ALBUM: IN GETJSON
D/RESPONSE ALBUM: hey alnull