java - JsonHttpRH : onSuccess(int, Header[], JSONArray) 未被覆盖,但收到回调?

标签 java android

我的应用程序应该显示来自 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 getActiveCurrenices() {
        return active_cryptocurrencies;
    }

    public String getMarkets() {
        return markets;
    }

    public String getTotalVolume() {
        return total_volume;
    }

    public String getMarketCapPercent() {
        return market_cap_percentage;
    }

}

最后,这个方法在我的 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);
            }
        });
    }

但它似乎不起作用,日志显示:JsonHttpRH: onSuccess(int, Header[], JSONArray) was not overrided, but callback was returned.

这是来自 API 的响应数据:https://api.coingecko.com/api/v3/global

最佳答案

尝试使用retrofit2。将这些依赖项添加到gradle中。 使用 Retrofit 是从远程源获取数据的最佳方式。

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

然后创建一个包含您的基本网址的 APIURL 类

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

之后创建一个 APIService 类,其中包含您的获取参数。我的数据应该从该网站生成。 http://www.jsonschema2pojo.org

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.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());
                }
            });
        }

关于java - JsonHttpRH : onSuccess(int, Header[], JSONArray) 未被覆盖,但收到回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079022/

相关文章:

java - @Nullable 和 SonarQube 'Conditionally executed blocks should be reachable' 警告

android - Twitter 登录 Quickblox

android - 开发适用于 Android 和桌面的 Air (Flex) 应用程序

java - 如何使用正则表达式从字符串中获取数字?

Android沉浸式下拉菜单,隐藏导航栏

java - MediaCodec H264 编码器不适用于 Snapdragon 800 设备

android - 如何减小 Android Apk 的大小

java - 位图创建空指针异常

java - thrift/java 中的异常类型层次结构

java - 使用java将html转换为xml