【发布时间】:2015-07-01 18:45:45
【问题描述】:
我想连接到可以有多个页面的 Api 链接,并将所有 JSON 值作为一个对象存储在一个列表中。
Here is a Api link example with multiple pages, note the number as last being the page you're on.
到目前为止我遇到但无法解决的问题。 doInBackground 作为构造函数类 apiRootObject 的返回类型以及如何反序列化 Json 结果,这是合乎逻辑的为什么它不起作用,因为我从 AsyncTask 扩展但我不知道如何解决这个问题或其他途径拿走。
这是我目前的代码。
在我的 Activity.java 中调用初始函数
String userSearchRequest = search_activity_data.getString("userSearchRequest");
String URL = "http://www.gw2spidy.com/api/v0.9/json/item-search/" + userSearchRequest + "/";
//Api link example with multiple pages = "http://www.gw2spidy.com/api/v0.9/json/item-search/Iron"
AsyncFetch parkingInfoFetch = new AsyncFetch(this);
parkingInfoFetch.setOnResponse(this);
parkingInfoFetch.execute(URL);
从上述代码调用的我的 AsyncFetch.java 类 公共类 AsyncFetch 扩展 AsyncTask {
public AsyncFetch(Context context) {
this.context = context;
}
private Context context;
private JSONObject jsonObject;
private onResponse onResponse;
public void setOnResponse(onResponse onResponse) {
this.onResponse = onResponse;
}
@Override
protected apiRootObject doInBackground(String... params) { //Incompatible return type
// TODO Auto-generated method stub
apiRootObject apiRootObject = null;
apiRootObject tempApiRootObject = null;
int page = 0;
try {
do {
HttpGet get = new HttpGet(params[0] + page);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//jsonObject = new JSONObject(result);
tempApiRootObject = /*Deserialize into <RootObject>(result)*/
if (apiRootObject == null){
apiRootObject = tempApiRootObject;
}
else{
apiRootObject.results.addAll(tempApiRootObject.results);
apiRootObject.count += tempApiRootObject.count;
}
page++;
}
while(tempApiRootObject.last_page != tempApiRootObject.page);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return apiRootObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
this.onResponse.onResponse(result);
}
public interface onResponse {
public void onResponse(JSONObject object);
}
}
然后返回到 activity.java,所有内容都被添加到 onResponse 函数的列表中。
public void onResponse(JSONObject object) {//Still expecting a JSONObject while I am changing this return type
Log.i("gw2Log", object.toString());
apiRootObject resultClass = new apiRootObject();
try {
resultClass.setCount(object.getInt("count"));
resultClass.setPage(object.getInt("page"));
resultClass.setLast_page(object.getInt("last_page"));
resultClass.setTotal(object.getInt("total"));
JSONArray list = new JSONArray(object.getString("results"));
for (int i = 0; i < resultClass.getCount(); i++) {
JSONObject resultsObject = list.getJSONObject(i);
apiResults temp = new apiResults();
temp.setData_id(resultsObject
.getInt("data_id"));
temp.setName(resultsObject
.getString("name"));
temp.setRarity(resultsObject
.getInt("rarity"));
temp.setRestriction_level(resultsObject
.getInt("restriction_level"));
temp.setImg(resultsObject
.getString("img"));
temp.setType_id(resultsObject
.getInt("type_id"));
temp.setSub_type_id(resultsObject
.getInt("sub_type_id"));
temp.setPrice_last_changed(resultsObject
.getString("price_last_changed"));
temp.setMax_offer_unit_price(resultsObject
.getInt("max_offer_unit_price"));
temp.setMin_sale_unit_price(resultsObject
.getInt("min_sale_unit_price"));
temp.setOffer_availability(resultsObject
.getInt("offer_availability"));
temp.setSale_availability(resultsObject
.getInt("sale_availability"));
temp.setSale_price_change_last_hour(resultsObject
.getInt("sale_price_change_last_hour"));
temp.setOffer_price_change_last_hour(resultsObject
.getInt("offer_price_change_last_hour"));
resultClass.addObject(temp);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0; i < resultClass.count; i++) {
Log.i("gw2Log", resultClass.getObject(i).name);
}
}
当然还有apiResults和apiRootObject两个构造函数类。
编辑: 如果您点击问题顶部的链接,您会返回大量 JSON 值,如果创建了更多新页面,则每个页面可以有 50 个这样的结果。
我想连接到这个 Api 链接,并检索所有返回的值。如果有多个页面,它需要遍历所有现有页面并将这些 JSON 值添加到完全相同的列表中。
我之前在 c# 中问过一个类似的问题,现在我已经让它工作了,但我现在需要在 Android Java 中完全相同。对于 android java,我被告知我需要使用 AsyncTask 来建立连接并在应用程序的后台执行所有这些操作。如果有更好或更简单的方法,请赐教。
【问题讨论】:
-
为什么要删除这个 jsonObject = new JSONObject(result);
-
@CharefEddineMechalikh 好吧,我在搞乱整个代码,老实说我不记得我为什么评论了,但我只是在发布这个问题之前才这样做,所以问题仍然存在。跨度>
-
帮我理解..你需要你的异步任务返回多个jsonObject吗?
-
@CharefEddineMechalikh 我已经在底部编辑了我的问题,希望这清楚。
-
是的,我尝试了链接,我看到了结果
标签: java android json list object