gpt4 book ai didi

java - Android Java 检索 JSON 值并将其作为对象存储在列表中

转载 作者:行者123 更新时间:2023-11-30 11:04:57 25 4
gpt4 key购买 nike

我想连接到可以有多个页面的 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 类公共(public)类 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);

}

}

当然还有2个构造器类apiResultsapiRootObject

编辑:如果您点击问题顶部的链接,您会返回很多 JSON 值,如果创建了更多的新页面,则每个页面都可以有 50 个这样的结果。

我想连接到这个 Api 链接,并检索所有返回的值。如果有多个页面,它需要遍历所有现有页面并将这些 JSON 值添加到完全相同的列表中。

我之前在 c# 中问过一个类似的问题,在这里我已经可以正常工作了,但我现在需要在 Android Java 中完全相同。对于 android java,我被告知我需要使用 AsyncTask 才能建立连接并在应用程序的后台执行所有这些操作。如果有更好或更简单的方法,请赐教。

最佳答案

使用Retrofit可能还不算太晚.

关于java - Android Java 检索 JSON 值并将其作为对象存储在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com