简体   繁体   中英

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM