英文:
JsonHttpRH: onSuccess(int, Header[], JSONArray) was not overriden, but callback was received?
问题
我的应用程序应该从CoinGecko API显示数据,以下是我尝试过的代码,但无法使其正常工作。
API服务:
public class CoinGeckoService {
public static final String COINGECKO_GLOBAL_URL = "https://api.coingecko.com/api/v3/global";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void getGlobalData(AsyncHttpResponseHandler responseHandler) {
List<NameValuePair> params = new ArrayList<>();
try{
URIBuilder query = new URIBuilder(COINGECKO_GLOBAL_URL);
query.addParameters(params);
client.get(query.toString(), responseHandler);
}
catch (URISyntaxException e){
Log.e("URISyntaxException", e.getMessage());
}
}
}
模型:
public class GlobalData implements Parcelable {
private String total_market_cap;
private String active_cryptocurrencies;
private String markets;
private String total_volume;
private String market_cap_percentage;
public GlobalData(Parcel in){
String[] data = new String[5];
in.readStringArray(data);
this.total_market_cap = data[0];
this.active_cryptocurrencies = data[1];
this.markets = data[2];
this.total_volume = data[3];
this.market_cap_percentage = data[4];
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<GlobalData> CREATOR = new Parcelable.Creator<GlobalData>() {
@Override
public GlobalData createFromParcel(Parcel source) {
return new GlobalData(source);
}
@Override
public GlobalData[] newArray(int size) {
return new GlobalData[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[]{
this.total_market_cap,
this.active_cryptocurrencies,
this.markets,
this.total_volume,
this.market_cap_percentage
});
}
public String getTotalMarketCap() {
return total_market_cap;
}
public String getActiveCurrencies() {
return active_cryptocurrencies;
}
public String getMarkets() {
return markets;
}
public String getTotalVolume() {
return total_volume;
}
public String getMarketCapPercent() {
return market_cap_percentage;
}
}
最后,在我的片段中有这个方法:
public void getGlobalData() {
CoinGeckoService.getGlobalData(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
GlobalData[] result = new Gson().fromJson(response.toString(), GlobalData[].class);
for (GlobalData node : result) {
marketCapData.setText(node.getTotalMarketCap());
Log.d("response_", "" + node.getTotalMarketCap());
}
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) {
swipeRefreshLayout.setRefreshing(false);
}
});
}
但是似乎不起作用,日志显示:JsonHttpRH:未覆盖onSuccess(int,Header[],JSONArray),但已收到回调。
这是来自API的响应数据:https://api.coingecko.com/api/v3/global
英文:
My app is supposed to show data from CoinGecko API, here's what I tried but can't get it to work.
API Service:
public class CoinGeckoService {
public static final String COINGECKO_GLOBAL_URL = "https://api.coingecko.com/api/v3/global";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void getGlobalData(AsyncHttpResponseHandler responseHandler) {
List<NameValuePair> params = new ArrayList<>();
try{
URIBuilder query = new URIBuilder(COINGECKO_GLOBAL_URL);
query.addParameters(params);
client.get(query.toString(), responseHandler);
}
catch (URISyntaxException e){
Log.e("URISyntaxException", e.getMessage());
}
}
}
Model:
public class GlobalData implements Parcelable {
private String total_market_cap;
private String active_cryptocurrencies;
private String markets;
private String total_volume;
private String market_cap_percentage;
public GlobalData(Parcel in){
String[] data = new String[5];
in.readStringArray(data);
this.total_market_cap = data[0];
this.active_cryptocurrencies = data[1];
this.markets = data[2];
this.total_volume = data[3];
this.market_cap_percentage = data[4];
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<GlobalData> CREATOR = new Parcelable.Creator<GlobalData>() {
@Override
public GlobalData createFromParcel(Parcel source) {
return new GlobalData(source);
}
@Override
public GlobalData[] newArray(int size) {
return new GlobalData[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[]{
this.total_market_cap,
this.active_cryptocurrencies,
this.markets,
this.total_volume,
this.market_cap_percentage
});
}
public String getTotalMarketCap() {
return total_market_cap;
}
public String getActiveCurrenices() {
return active_cryptocurrencies;
}
public String getMarkets() {
return markets;
}
public String getTotalVolume() {
return total_volume;
}
public String getMarketCapPercent() {
return market_cap_percentage;
}
}
Lastly, this method is inside my fragment:
public void getGlobalData() {
CoinGeckoService.getGlobalData(new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
GlobalData[] result = new Gson().fromJson(response.toString(), GlobalData[].class);
for (GlobalData node : result) {
marketCapData.setText(node.getTotalMarketCap());
Log.d("response_", "" +node.getTotalMarketCap());
}
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) {
swipeRefreshLayout.setRefreshing(false);
}
});
}
But It doesn't seem to work, and log says: JsonHttpRH: onSuccess(int, Header[], JSONArray) was not overriden, but callback was received.
This is the response data from the API: https://api.coingecko.com/api/v3/global
答案1
得分: 1
以下是翻译好的部分:
尝试使用retrofit2。将以下依赖项添加到gradle中。
使用retrofit是从远程源获取数据的最佳方式。
implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
然后创建一个APIURL类,其中包括您的基本URL。
public class APIUrl {
public static final String BASE_URL = "https://api.coingecko.com/";
}
之后创建一个APIService类,其中包含您的获取参数。我的数据应该从这个网站生成。
public interface APIService {
@GET("api/v3/global")
Call<MyData> getList();
}
最后像这样获取您的数据。
public void getData() {
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
APIService apiService = retrofit.create(APIService.class);
Call<MyData> call = apiService.getList();
call.enqueue(new Callback<MyData>() {
@Override
public void onResponse(Call<MyData> call, Response<MyData> response) {
// 这将以MyData类型提供所有数据,您可以在此处进行操作
}
@Override
public void onFailure(Call<MyData> call, Throwable t) {
Log.d("error", t.getMessage().toString());
}
});
}
英文:
try to use retrofit2.Add these dependencies into gradle.
Using retrofit is the best way for fetching data from remote source.
implementation 'com.squareup.retrofit2:retrofit:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
then create an APIURL class which includes your base url
public class APIUrl {
public static final String BASE_URL = "https://api.coingecko.com/";
}
after that create an APIService class which includes your get parameters.My data should be generated from this website.
http://www.jsonschema2pojo.org
public interface APIService {
@GET("api/v3/global")
Call<MyData> getList();
}
Lastly fetch your data like this.
public void getData() {
Gson gson = new GsonBuilder().setLenient().create();
Retrofit retrofit = new Retrofit.Builder().baseUrl(APIUrl.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
APIService apiService = retrofit.create(APIService.class);
Call<MyData> call = apiService.getData();
call.enqueue(new Callback<VeriListem>() {
@Override
public void onResponse(Call< MyData > call, Response< MyData > response) {
// this gives you all data as MyData type and do your operation here
}
@Override
public void onFailure(Call<MyData> call, Throwable t) {
Log.d("error", t.getMessage().toString());
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论