0

I would like to connect to a Api url, retrieve the json and store everything in a object list. Here is an example of what the url can return as Json.

The following code was given to me but it returns a error Cannot resolve method setOnResponse in my activity line 31

This is my 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 class

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

And ofcourse the constructur ResultClass which i assume is not necessary to include here as code.

What does this error Cannot resolve method setOnResponse mean and how do i fix this?

1 Answer 1

1

Change this line:

AsyncTask parkingInfoFetch = new AsyncFetch(this);

To this:

AsyncFetch parkingInfoFetch = new AsyncFetch(this);

The error means that the line:

parkingInfoFetch.setOnResponse(this);

Is trying to call a method defined in the subclass AsyncFetch, but you have the variable defined as the parent class AsyncTask which has no method setOnResponse.

Sign up to request clarification or add additional context in comments.

8 Comments

sigh... i feel like a retard now.. Thanks a lot for the help, i bet I would be stuck for another day otherwise! Could you also tell me how and where I can access my object?
The AsyncFetch class is using a callback method that gets called when the request is done. That method is the onResponse method in your Activity. There you are parsing the JSON, although it looks like you have some issues parsing it: JSONArray array = new JSONArray(object.getString("results")); Should be: JSONArray array = new object.getArray("results"); Unless the original JSON is double encoded for the results node. Also, you should be instantiating a new ResultClass for each record and putting them in a list. Finally the list should be a member variable of the Activity.
it doesn't recognize .getArray() also if i were to access my object would it be something like this or? e.x. logging a property ` Log.i("gw2Log", ResultClass.Count[0]);`?
Sorry, the method is getJSONArray not getArray. ResultClass.Count[0] has some issues. First your variable name is resultClass not ResultClass and also, like I said before it should be a List that you are using to store your results. Take a look here pastebin.com/iLBLmbur
That was just a code snippet to help you see the direction to go. You already had a try/catch in your original code, this code should go inside of a similar try/catch statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.