1
我有一個ListFragment需要一個URL來解析JSON使用GSON庫。對於抽射後臺任務:Android - GsonRequest返回null
public class LatestFragment extends ListFragment {
public LatestFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_latest, container,
false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
arrItemList = new ArrayList<ItemListModel>();
va = new LatestAdapter(getActivity(), arrItemList);
loadItemList(1);
}
private void loadItemList(int page) {
mRequestQueue = Volley.newRequestQueue(getActivity());
pd = ProgressDialog.show(getActivity(), null, "Loading...");
// TODO use Volley param
currentPage = page;
param = "?page=" + currentPage;
String url = Constants.LATEST_ITEM_LIST + param;
GsonRequest<ItemListModel> myReq = new GsonRequest<ItemListModel>(
Method.GET, url, ItemListModel.class,
createMyReqSuccessListener(), createMyReqErrorListener());
mRequestQueue.add(myReq);
}
private Response.Listener<ItemListModel> createMyReqSuccessListener() {
return new Response.Listener<ItemListModel>() {
@Override
public void onResponse(ItemListModel response) {
try {
Log.d("response > ", response.toString());
pd.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
};
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
};
}
}
當我運行應用程序,它顯示的是response
空。
這裏是ItemListModel
:
public class ItemListModel {
private String user_id;
private String item_id;
private String name;
private String price;
private String category;
private ThumbnailModel thumbnail; // not array list
public ItemListModel(){}
//getters
@Override
public String toString() {
return "ItemListModel [user_id=" + user_id + ", item_id=" + item_id
+ ", name=" + name + ", price=" + price + ", category="
+ category + ", thumbnail=" + thumbnail + "]";
} // ALL NULL
}
這反序列化這個JSON格式:
{
"results": [
{
"user_id": "1",
"item_id": "18630",
"name": "Unnamed Item",
"price": "0",
"description": "",
"created_at": "2014-01-16 15:31:36",
"thumbnail": {
"image50": "http://www.example.com/adsa.jpg",
"image100": "hhttp://www.example.com/adsa.jpg"
},...
我做了什麼錯了?我應該堅持使用Volley的JSONArrayRequest嗎?嗯。
嘿你的解決方案的作品。但是,如何從'Result'和'ThumbnailModel'類中獲取String值? – AimanB
@AimanB如果它有效,那麼不要忘記投票並接受答案。 –
@AimanBI添加了獲取Result和ThumbnailModel類的代碼。再次檢查我的答案。 –