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

标签 java android json parsing object

我想连接到 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/

相关文章:

java - Android - 新 Intent 启动特定方法

android - 在开发 Android 应用程序时使用 Linux 操作系统重要吗?

java - 更改场景时出现 ArrayIndexOutOfBoundsException

java - 在用户触摸屏幕的地方绘制圆圈

json - 为 Busted lua 单元测试创​​建 JSON 输出时出现问题

javascript - 将 JS 对象从一个 HTML 页面发送到另一个

json - 在 Angular 中,作为索引类型导入的 json 文件总是返回 undefined

java - Maven 和 graphql-java

java - 子类构造函数

java - 如何修复 'android.os.NetworkOnMainThreadException' ?