JsonHttpRH: `onSuccess(int, Header[], JSONArray)`没有被重写,但是收到了回调?

huangapple go评论81阅读模式
英文:

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 = &quot;https://api.coingecko.com/api/v3/global&quot;;
private static AsyncHttpClient client = new AsyncHttpClient();
public static void getGlobalData(AsyncHttpResponseHandler responseHandler) {
List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
try{
URIBuilder query = new URIBuilder(COINGECKO_GLOBAL_URL);
query.addParameters(params);
client.get(query.toString(), responseHandler);
}
catch (URISyntaxException e){
Log.e(&quot;URISyntaxException&quot;, 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&lt;GlobalData&gt; CREATOR = new Parcelable.Creator&lt;GlobalData&gt;() {
@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(&quot;response_&quot;, &quot;&quot; +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 &#39;com.squareup.retrofit2:retrofit:2.2.0&#39;
implementation &#39;com.squareup.retrofit2:converter-gson:2.2.0&#39;

then create an APIURL class which includes your base url

  public class APIUrl {
public static final String BASE_URL = &quot;https://api.coingecko.com/&quot;;
}

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(&quot;api/v3/global&quot;)
Call&lt;MyData&gt; 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&lt;MyData&gt; call = apiService.getData();
call.enqueue(new Callback&lt;VeriListem&gt;() {
@Override
public void onResponse(Call&lt; MyData &gt; call, Response&lt; MyData &gt; response) {
// this gives you all data as MyData type and do your operation here
}
@Override
public void onFailure(Call&lt;MyData&gt; call, Throwable t) {
Log.d(&quot;error&quot;, t.getMessage().toString());
}
});
}

huangapple
  • 本文由 发表于 2020年4月7日 19:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61079022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定