【问题标题】:JsonHttpRH: onSuccess(int, Header[], JSONArray) was not overriden, but callback was received?JsonHttpRH:onSuccess(int, Header[], JSONArray) 没有被覆盖,但是收到了回调?
【发布时间】:2020-04-07 11:29:39
【问题描述】:

我的应用应该显示来自 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;
    }

}

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

    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 override, but callback was received。

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

【问题讨论】:

    标签: java android


    【解决方案1】:

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

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

    然后创建一个包含您的基本 url 的 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());
                    }
                });
            }
    

    【讨论】:

    • 对于创建 MyData 粘贴您的 json 响应 jsonschema2pojo.org 并选择 Source type: JSON, Annotation style:GSON。
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多