gpt4 book ai didi

java - Android java解析Json从url到对象列表

转载 作者:行者123 更新时间:2023-12-01 11:40:14 25 4
gpt4 key购买 nike

我想连接到 Api url,检索 json 并将所有内容存储在对象列表中。 Here is an example url 可以以 Json 形式返回的内容。

向我提供了以下代码,但它返回错误无法解析我的 Activity 第31行中的方法setOnResponse

这是我的activity.java

public class resultOverview_activity extends Activity implements onResponse{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_overview);

Bundle search_activity_data = getIntent().getExtras();
if(search_activity_data == null){
return;
}

String URL = "http://www.gw2spidy.com/api/v0.9/json/item-search/Sunrise";
AsyncTask parkingInfoFetch = new AsyncFetch(this);
parkingInfoFetch.setOnResponse(this);
parkingInfoFetch.execute(URL);

//Log.i("gw2Log", parkingInfoFetch.);


}

@Override
public void onResponse(JSONObject object) {
Log.d("Json Response", "Json Response" + object);

ResultClass resultClass = new ResultClass();

try {
resultClass.setCount(object.getInt("count"));
resultClass.setPage(object.getInt("page"));
resultClass.setLast_page(object.getInt("last_page"));
resultClass.setTotal(object.getInt("total"));
JSONArray array = new JSONArray(object.getString("results"));
for (int i = 0; i < resultClass.getTotal(); i++) {
JSONObject resultsObject = array.getJSONObject(i);
resultClass.setData_id(resultsObject.getInt("data_id"));
resultClass.setName(resultsObject.getString("name"));
resultClass.setRarity(resultsObject.getInt("rarity"));
resultClass.setRestriction_level(resultsObject
.getInt("restriction_level"));
resultClass.setImg(resultsObject.getString("img"));
resultClass.setType_id(resultsObject.getInt("type_id"));
resultClass.setSub_type_id(resultsObject.getInt("sub_type_id"));
resultClass.setPrice_last_changed(resultsObject
.getString("price_last_changed"));
resultClass.setMax_offer_unit_price(resultsObject
.getInt("max_offer_unit_price"));
resultClass.setMin_sale_unit_price(resultsObject
.getInt("min_sale_unit_price"));
resultClass.setOffer_availability(resultsObject
.getInt("offer_availability"));
resultClass.setSale_availability(resultsObject
.getInt("sale_availability"));
resultClass.setSale_price_change_last_hour(resultsObject
.getInt("sale_price_change_last_hour"));
resultClass.setOffer_price_change_last_hour(resultsObject
.getInt("offer_price_change_last_hour"));

}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

AsyncFetch.java 类

public class AsyncFetch extends AsyncTask<String, Void, JSONObject> {

public AsyncFetch(Context context) {
this.context = context;
}

private Context context;
private JSONObject jsonObject;
private onResponse onResponse;

public onResponse getOnResponse() {
return onResponse;
}

public void setOnResponse(onResponse onResponse) {
this.onResponse = onResponse;
}

@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
try {
HttpGet get = new HttpGet(params[0]);
HttpClient client = new DefaultHttpClient();

HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
jsonObject = new JSONObject(result);
} 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 jsonObject;
}

@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);
}
}

当然还有constructur ResultClass,我认为没有必要将其作为代码包含在此处。

此错误无法解析方法 setOnResponse 是什么意思以及如何解决此问题?

最佳答案

更改此行:

AsyncTask parkingInfoFetch = new AsyncFetch(this);

对此:

AsyncFetch parkingInfoFetch = new AsyncFetch(this);

该错误意味着该行:

parkingInfoFetch.setOnResponse(this);

正在尝试调用子类AsyncFetch中定义的方法,但您将变量定义为父类AsyncTask,它没有方法setOnResponse.

关于java - Android java解析Json从url到对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29588667/

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