我无法使用JSON和Volley将请求中的数据共享给其他android活动

测试测试

我正在用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);
    
    }
    
Om Infowave开发人员

有一个错误getMarketSummary()总是返回空列表。因此使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);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在Android中[使用Volley Library]提取JSON数据

无法使用公共共享服务在其他控制器中检索数据

我想取消从Android中其他活动创建的警报

如何根据意图数据在android中启动其他活动

Android:Listview数据未在其他活动中传递

使用 Beautiful Soup 抓取网页,无法从 application/json 数据中获取序列化日期和其他键值对

如何在Android中的片段和活动之间共享数据

Android:AlarmManager和PendingIntent不会从其他活动中取消

我无法通过意图发送数据的其他活动来启动活动

如何使用 DJANGO 上的 POST 请求将默认数据保存在其他字段中?

与其他组件共享JSON数据

扩展基本活动会使我的其他活动无法使用

如何从Android ..中的其他活动中的活动中获取数据?

使用Pandas将数据框和其他数据保存在同一.csv文件中

使用 Android Volley 发布和获取 Json 数据

无法从Android中的一项活动转到其他活动

在其他活动中编辑共享首选项

共享我的URL时,Facebook Sharer如何选择图像和其他元数据?

来自SQLite数据库的数据保存在一个活动中,无法在其他活动中显示

使用骨架和其他服务器脚本语言将数据存储在mysql中

如何使用Android Volley将数据发布到mysql中

返回Android堆栈中的其他活动

无法在Android数据绑定中引用其他View ID

Android Wear 2.0是否支持唤醒其他独立应用程序和共享数据的Intent?

使用其他活动中的方法

将数据从服务发送到其他活动的BroadcastReceiver(Xamarin Android)

使用python中的请求函数解析json数据。...我无法访问对象

如果将代码共享给其他系统,我是否需要在代码中安装 alamofire

在dat协议中,如果我安装dat并使用它共享文件夹,那么我将成为所谓的对等方并存储其他对等方的数据,对吗?