我无法与JSON和Volley的请求共享数据到其他Android活动

时间:2018-01-04 09:48:22

标签: android callback android-volley jsonobjectrequest

我在java类中使用JsonObjectRequest和Volley发出请求,一旦我获得数据,我就无法将它发送到我需要使用它的活动。我尝试使用回调,但我不知道我做错了什么。我尝试了几件事,但都没有奏效。我在请求类中正确获取数据,因此问题是从活动中获取数据。

我认为我的问题与回调有关,但正如我所说,我已尽力而为。

任何帮助将不胜感激!

  • 这是我的请求代码:

    public ArrayList<Coin> getMarketSummary(final DashboardActivity.CoinCallback callback, ArrayList<Coin> listAux, Context context) {
    
    Log.d("chegamos a entrar en getCOinData??", "Entramos en getMarketSummary");
    
    listCoins.clear();
    
    requestQueue = Volley.newRequestQueue(context);
    
    for (Coin coinAux : listAux) {
        this.coin = coinAux;
    
        if (!coin.getShortName().equals("BTC")) {
            //we create the URL for request the market
            String urlMarket = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-SHORTNAME";
    
            String coinShortName = coin.getShortName();
    
            urlMarket = urlMarket.replaceAll("SHORTNAME", coinShortName.toLowerCase());
    
            //once created the url, we create the request with JSONObject
    
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlMarket, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
    
                    try {
    
                        JSONArray result = response.getJSONArray("result");
                        //we loop the response
                        for (int i = 0; i < result.length(); i++) {
                            coin.setHigh(Double.parseDouble(result.getJSONObject(i).getString("High")));
    
                            coin.setLow(Double.parseDouble(result.getJSONObject(i).getString("Low")));
                            coin.setLast(Double.parseDouble(result.getJSONObject(i).getString("Last")));
                            coin.setVolInBtc(Double.parseDouble(result.getJSONObject(i).getString("BaseVolume")));
                            coin.setBid(Double.parseDouble(result.getJSONObject(i).getString("Bid")));
                            coin.setAsk(Double.parseDouble(result.getJSONObject(i).getString("Ask")));
                            coin.setPrevDay(result.getJSONObject(i).getString("PrevDay"));
    
                            listCoins.add(coin);
                            callback.onSuccess(listCoins);
    
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
    
            requestQueue.add(request);
        }
    }
    
    return listCoins;
    }
    
  • 这是我初始化回调的方式(在发出请求之前):

    public void initCallback() {
    
    this.coinCallback = new CoinCallback() {
        @Override
        public void onSuccess(ArrayList<Coin> coinListFromRequest) {
            coinList=coinListFromRequest;
        }
    };
    }
    
  • 以下是我调用请求的方法(初始化回调后):

    coinList = bittrexAPIRequest.getMarketSummary(coinCallback, coinList, this);
    adapter.notifyDataSetChanged();
    
  • 最后,我的CoinCallback界面:

    public interface CoinCallback {
    void onSuccess(ArrayList<Coin> coinList);
    
    }
    

1 个答案:

答案 0 :(得分:0)

错误getMarketSummary()总是返回Empty list.so使getMarketSummary返回类型为void并传递CoinCallback接口中的列表。

  public void getMarketSummary(final DashboardActivity.CoinCallback callback, ArrayList<Coin> listAux, Context context) {

        Log.d("chegamos a entrar en getCOinData??", "Entramos en getMarketSummary");

        listCoins.clear();

        requestQueue = Volley.newRequestQueue(context);

        for (Coin coinAux : listAux) {
            this.coin = coinAux;

            if (!coin.getShortName().equals("BTC")) {
                //we create the URL for request the market
                String urlMarket = "https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-SHORTNAME";

                String coinShortName = coin.getShortName();

                urlMarket = urlMarket.replaceAll("SHORTNAME", coinShortName.toLowerCase());

                //once created the url, we create the request with JSONObject

                JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlMarket, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {

                            JSONArray result = response.getJSONArray("result");
                            //we loop the response
                            for (int i = 0; i < result.length(); i++) {
                                coin.setHigh(Double.parseDouble(result.getJSONObject(i).getString("High")));

                                coin.setLow(Double.parseDouble(result.getJSONObject(i).getString("Low")));
                                coin.setLast(Double.parseDouble(result.getJSONObject(i).getString("Last")));
                                coin.setVolInBtc(Double.parseDouble(result.getJSONObject(i).getString("BaseVolume")));
                                coin.setBid(Double.parseDouble(result.getJSONObject(i).getString("Bid")));
                                coin.setAsk(Double.parseDouble(result.getJSONObject(i).getString("Ask")));
                                coin.setPrevDay(result.getJSONObject(i).getString("PrevDay"));

                                listCoins.add(coin);


                            }
                           callback.onSuccess(listCoins);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });

                requestQueue.add(request);
            }
        }

        //return listCoins;
        }

更新iniCallBack,现在更新的列表将在onSuccess中返回,您也可以在for中调用服务器,它可以多次调用相同的服务。

  public void initCallback() {
    coinList =new ArrayList();
    adapter =new Adapter(coinList);
    list.setAdapter(adapter);

    this.coinCallback = new CoinCallback() {
        @Override
        public void onSuccess(ArrayList<Coin> coinList) {
           coinList.addAll(coinList);
            adapter.notifyDataSetChanged();
        }
    };

    bittrexAPIRequest.getMarketSummary(coinCallback, coinList, this);
    }