0

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

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

2 Answers2

1

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();
            }
        }
    }
});
rhari
  • 1,367
  • 1
  • 11
  • 19
  • This seems to work as when I log the contents of `mStockList` in the loop it holds the correct data. However when I return the the contents of `mStockList` outside of this block, it is empty. – Tom Finet Nov 27 '16 at 22:31
  • It's an asynchronous call, the onSucess method runs in another thread and your other statements are in the UI thread, so the data will not have been loaded when your call is executed. – rhari Nov 27 '16 at 22:33
  • Thanks dude, life saving stuff. – Tom Finet Nov 27 '16 at 22:37
  • Accept it if it is what you are looking for :) – rhari Nov 27 '16 at 22:38
0

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.

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • Have you been to the url I listed in the question, isnt the api responding with a JSON array and not an object. Plus there is a method that contains JSON array as one of the paramaters that I can override.@Ascorbin – Tom Finet Nov 27 '16 at 21:18
  • If I change to your suggestion I get the following output in the console: `onSuccess(int, Header[], JSONObject) was not overriden, but callback was received 11-27 22:08:05.409 27015-27015/com.neutronstar.thecaffapi W/System.err: org.json.JSONException: No value for id` – Tom Finet Nov 27 '16 at 21:20