Tom Finet
Tom Finet

Reputation: 2136

Failing to make call to API from android client

I have a flask RESTFUL API which is fully functioning and deployed to heroku. I now need to make a get request from my android app. I am using the library to do this.

Here is the code which makes the call to the API at the url https://thecaffapi.herokuapp.com/api/v1/all_stock, which returns a JSON array to the android client:

client.get(baseurl + "all_stock", null, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray allStock) {
            for (int i = 0; i < allStock.length(); i++) {
                try {
                    JSONObject stockObject = allStock.getJSONObject(i);
                    int id = stockObject.getInt("id");
                    String stockTitle = stockObject.getString("stock_title");
                    int stockNumber = stockObject.getInt("stock_number");
                    Stock stock = new Stock(id, stockTitle, stockNumber);
                    mStockList.add(stock);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    });

The line client.get(baseurl + "all_stock", null, new JsonHttpResponseHandler() { is run, however the onSuccess() method does not run.

When the app is run the console reports the following related to the AsyncHttpClient:

V/AsyncHttpRH: Progress 216 from 216 (100%)
W/JsonHttpRH: onSuccess(int, Header[], JSONObject) was not overriden, but callback was received

Why is the onSuccess() method not being overriden and consequently run?

Thanks in advance,

Tom

Upvotes: 0

Views: 78

Answers (2)

rhari
rhari

Reputation: 1367

The third element of your onSucess method is the object to which your response should be parsed. Your response is not in an array format, it has a single name value pair where the value is an array. So, you need to use a JSONObject instead and get the array from that.

client.get(baseurl + "all_stock", null, new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        JSONArray allStock = response.getJSONArray("stock");
        for (int i = 0; i < allStock.length(); i++) {
            try {
                JSONObject stockObject = allStock.getJSONObject(i);
                int id = stockObject.getInt("id");
                String stockTitle = stockObject.getString("stock_title");
                int stockNumber = stockObject.getInt("stock_number");
                Stock stock = new Stock(id, stockTitle, stockNumber);
                mStockList.add(stock);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
});

Upvotes: 1

fweigl
fweigl

Reputation: 22038

Seems like you have to override the exact method

onSuccess(int, Header[], JSONObject), 

yours looks a bit different:

onSuccess(int statusCode, Header[] headers, JSONArray allStock) 

Your api gives you a JSONObject, not a JSONArray.

Upvotes: 0

Related Questions