I am making a request with JsonObjectRequest and Volley in a java class and, once I get the data, I can't send it to the activity where I need to use it. I tried using a callback but I don't know what I am doing wrong. I've tried several things but none of them worked. I get the data correctly in my request class so the problem is to get that data from the activity.
I think my problem is related with the callback but, as I said, I've tried everything I could.
Any help would be appreciated!
This is my request code:
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;
}
This is how I initialize the callback (before making the request):
public void initCallback() {
this.coinCallback = new CoinCallback() {
@Override
public void onSuccess(ArrayList<Coin> coinListFromRequest) {
coinList=coinListFromRequest;
}
};
}
Here is how I call the request (after I initialize the callback):
coinList = bittrexAPIRequest.getMarketSummary(coinCallback, coinList, this);
adapter.notifyDataSetChanged();
For last, my CoinCallback interface:
public interface CoinCallback {
void onSuccess(ArrayList<Coin> coinList);
}
There is mistake getMarketSummary() always return Empty list.so made getMarketSummary return type void and pass the list in CoinCallback interface.
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;
}
update the iniCallBack
, now updated list be return in onSuccess
also you have call the server in for
it may call the same service multiple time.
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);
}