我无法与 JSON 和 Volley 共享来自请求的数据给其他 android activity
I can't share data from request with JSON and Volley to other android activity
我正在 java class 中使用 JsonObjectRequest 和 Volley 发出请求,一旦我获得数据,我就无法将其发送到我需要的 activity使用它。我尝试使用回调,但我不知道我做错了什么。我已经尝试了几件事,但 none 成功了。我在请求 class 中正确获取了数据,所以问题是从 activity.
获取数据
我认为我的问题与回调有关,但正如我所说,我已尽我所能。
如有任何帮助,我们将不胜感激!
这是我的请求代码:
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() 总是有错误 return 空 list.so 使 getMarketSummary return 类型无效并在 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
中的 return 您还调用了 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);
}
我正在 java class 中使用 JsonObjectRequest 和 Volley 发出请求,一旦我获得数据,我就无法将其发送到我需要的 activity使用它。我尝试使用回调,但我不知道我做错了什么。我已经尝试了几件事,但 none 成功了。我在请求 class 中正确获取了数据,所以问题是从 activity.
获取数据我认为我的问题与回调有关,但正如我所说,我已尽我所能。
如有任何帮助,我们将不胜感激!
这是我的请求代码:
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() 总是有错误 return 空 list.so 使 getMarketSummary return 类型无效并在 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
中的 return 您还调用了 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);
}