我正在用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);
}
有一个错误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] 删除。
我来说两句