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

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

JsonHttpRH: onSuccess(int, Header[], JSONArray) was not overriden, but callback was received?

问题

我的应用程序应该从CoinGecko API显示数据,以下是我尝试过的代码,但无法使其正常工作。

API服务:

  1. public class CoinGeckoService {
  2. public static final String COINGECKO_GLOBAL_URL = "https://api.coingecko.com/api/v3/global";
  3. private static AsyncHttpClient client = new AsyncHttpClient();
  4. public static void getGlobalData(AsyncHttpResponseHandler responseHandler) {
  5. List<NameValuePair> params = new ArrayList<>();
  6. try{
  7. URIBuilder query = new URIBuilder(COINGECKO_GLOBAL_URL);
  8. query.addParameters(params);
  9. client.get(query.toString(), responseHandler);
  10. }
  11. catch (URISyntaxException e){
  12. Log.e("URISyntaxException", e.getMessage());
  13. }
  14. }
  15. }

模型:

  1. public class GlobalData implements Parcelable {
  2. private String total_market_cap;
  3. private String active_cryptocurrencies;
  4. private String markets;
  5. private String total_volume;
  6. private String market_cap_percentage;
  7. public GlobalData(Parcel in){
  8. String[] data = new String[5];
  9. in.readStringArray(data);
  10. this.total_market_cap = data[0];
  11. this.active_cryptocurrencies = data[1];
  12. this.markets = data[2];
  13. this.total_volume = data[3];
  14. this.market_cap_percentage = data[4];
  15. }
  16. @Override
  17. public int describeContents() {
  18. return 0;
  19. }
  20. public static final Parcelable.Creator<GlobalData> CREATOR = new Parcelable.Creator<GlobalData>() {
  21. @Override
  22. public GlobalData createFromParcel(Parcel source) {
  23. return new GlobalData(source);
  24. }
  25. @Override
  26. public GlobalData[] newArray(int size) {
  27. return new GlobalData[size];
  28. }
  29. };
  30. @Override
  31. public void writeToParcel(Parcel dest, int flags) {
  32. dest.writeStringArray(new String[]{
  33. this.total_market_cap,
  34. this.active_cryptocurrencies,
  35. this.markets,
  36. this.total_volume,
  37. this.market_cap_percentage
  38. });
  39. }
  40. public String getTotalMarketCap() {
  41. return total_market_cap;
  42. }
  43. public String getActiveCurrencies() {
  44. return active_cryptocurrencies;
  45. }
  46. public String getMarkets() {
  47. return markets;
  48. }
  49. public String getTotalVolume() {
  50. return total_volume;
  51. }
  52. public String getMarketCapPercent() {
  53. return market_cap_percentage;
  54. }
  55. }

最后,在我的片段中有这个方法:

  1. public void getGlobalData() {
  2. CoinGeckoService.getGlobalData(new JsonHttpResponseHandler() {
  3. @Override
  4. public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
  5. GlobalData[] result = new Gson().fromJson(response.toString(), GlobalData[].class);
  6. for (GlobalData node : result) {
  7. marketCapData.setText(node.getTotalMarketCap());
  8. Log.d("response_", "" + node.getTotalMarketCap());
  9. }
  10. swipeRefreshLayout.setRefreshing(false);
  11. }
  12. @Override
  13. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) {
  14. swipeRefreshLayout.setRefreshing(false);
  15. }
  16. });
  17. }

但是似乎不起作用,日志显示: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:

  1. public class CoinGeckoService {
  2. public static final String COINGECKO_GLOBAL_URL = &quot;https://api.coingecko.com/api/v3/global&quot;;
  3. private static AsyncHttpClient client = new AsyncHttpClient();
  4. public static void getGlobalData(AsyncHttpResponseHandler responseHandler) {
  5. List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
  6. try{
  7. URIBuilder query = new URIBuilder(COINGECKO_GLOBAL_URL);
  8. query.addParameters(params);
  9. client.get(query.toString(), responseHandler);
  10. }
  11. catch (URISyntaxException e){
  12. Log.e(&quot;URISyntaxException&quot;, e.getMessage());
  13. }
  14. }
  15. }

Model:

  1. public class GlobalData implements Parcelable {
  2. private String total_market_cap;
  3. private String active_cryptocurrencies;
  4. private String markets;
  5. private String total_volume;
  6. private String market_cap_percentage;
  7. public GlobalData(Parcel in){
  8. String[] data = new String[5];
  9. in.readStringArray(data);
  10. this.total_market_cap = data[0];
  11. this.active_cryptocurrencies = data[1];
  12. this.markets = data[2];
  13. this.total_volume = data[3];
  14. this.market_cap_percentage = data[4];
  15. }
  16. @Override
  17. public int describeContents() {
  18. return 0;
  19. }
  20. public static final Parcelable.Creator&lt;GlobalData&gt; CREATOR = new Parcelable.Creator&lt;GlobalData&gt;() {
  21. @Override
  22. public GlobalData createFromParcel(Parcel source) {
  23. return new GlobalData(source);
  24. }
  25. @Override
  26. public GlobalData[] newArray(int size) {
  27. return new GlobalData[size];
  28. }
  29. };
  30. @Override
  31. public void writeToParcel(Parcel dest, int flags) {
  32. dest.writeStringArray(new String[]{
  33. this.total_market_cap,
  34. this.active_cryptocurrencies,
  35. this.markets,
  36. this.total_volume,
  37. this.market_cap_percentage
  38. });
  39. }
  40. public String getTotalMarketCap() {
  41. return total_market_cap;
  42. }
  43. public String getActiveCurrenices() {
  44. return active_cryptocurrencies;
  45. }
  46. public String getMarkets() {
  47. return markets;
  48. }
  49. public String getTotalVolume() {
  50. return total_volume;
  51. }
  52. public String getMarketCapPercent() {
  53. return market_cap_percentage;
  54. }
  55. }

Lastly, this method is inside my fragment:

  1. public void getGlobalData() {
  2. CoinGeckoService.getGlobalData(new JsonHttpResponseHandler() {
  3. @Override
  4. public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
  5. GlobalData[] result = new Gson().fromJson(response.toString(), GlobalData[].class);
  6. for (GlobalData node : result) {
  7. marketCapData.setText(node.getTotalMarketCap());
  8. Log.d(&quot;response_&quot;, &quot;&quot; +node.getTotalMarketCap());
  9. }
  10. swipeRefreshLayout.setRefreshing(false);
  11. }
  12. @Override
  13. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject response) {
  14. swipeRefreshLayout.setRefreshing(false);
  15. }
  16. });
  17. }

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是从远程源获取数据的最佳方式。

  1. implementation 'com.squareup.retrofit2:retrofit:2.2.0'
  2. implementation 'com.squareup.retrofit2:converter-gson:2.2.0'

然后创建一个APIURL类,其中包括您的基本URL。

  1. public class APIUrl {
  2. public static final String BASE_URL = "https://api.coingecko.com/";
  3. }

之后创建一个APIService类,其中包含您的获取参数。我的数据应该从这个网站生成。

  1. public interface APIService {
  2. @GET("api/v3/global")
  3. Call<MyData> getList();
  4. }

最后像这样获取您的数据。

  1. public void getData() {
  2. Gson gson = new GsonBuilder().setLenient().create();
  3. Retrofit retrofit = new Retrofit.Builder()
  4. .baseUrl(APIUrl.BASE_URL)
  5. .addConverterFactory(GsonConverterFactory.create(gson))
  6. .build();
  7. APIService apiService = retrofit.create(APIService.class);
  8. Call<MyData> call = apiService.getList();
  9. call.enqueue(new Callback<MyData>() {
  10. @Override
  11. public void onResponse(Call<MyData> call, Response<MyData> response) {
  12. // 这将以MyData类型提供所有数据,您可以在此处进行操作
  13. }
  14. @Override
  15. public void onFailure(Call<MyData> call, Throwable t) {
  16. Log.d("error", t.getMessage().toString());
  17. }
  18. });
  19. }
英文:

try to use retrofit2.Add these dependencies into gradle.
Using retrofit is the best way for fetching data from remote source.

  1. implementation &#39;com.squareup.retrofit2:retrofit:2.2.0&#39;
  2. implementation &#39;com.squareup.retrofit2:converter-gson:2.2.0&#39;

then create an APIURL class which includes your base url

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

after that create an APIService class which includes your get parameters.My data should be generated from this website.
http://www.jsonschema2pojo.org

  1. public interface APIService {
  2. @GET(&quot;api/v3/global&quot;)
  3. Call&lt;MyData&gt; getList();
  4. }

Lastly fetch your data like this.

  1. public void getData() {
  2. Gson gson = new GsonBuilder().setLenient().create();
  3. Retrofit retrofit = new Retrofit.Builder().baseUrl(APIUrl.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).build();
  4. APIService apiService = retrofit.create(APIService.class);
  5. Call&lt;MyData&gt; call = apiService.getData();
  6. call.enqueue(new Callback&lt;VeriListem&gt;() {
  7. @Override
  8. public void onResponse(Call&lt; MyData &gt; call, Response&lt; MyData &gt; response) {
  9. // this gives you all data as MyData type and do your operation here
  10. }
  11. @Override
  12. public void onFailure(Call&lt;MyData&gt; call, Throwable t) {
  13. Log.d(&quot;error&quot;, t.getMessage().toString());
  14. }
  15. });
  16. }

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:

确定