G0D
V2EX  ›  Android

怎么查找 Retrofit 请求失败的原因呢

  •  
  •   G0D · Aug 31, 2016 · 16879 views
    This topic created in 3717 days ago, the information mentioned may be changed or developed.

    请求失败,又不知道什么原因,该怎么解决

    private void getData() {
        String baseUrl1 = "http://demo.wp-api.org/";
        Retrofit retrofit1 = new Retrofit.Builder()
                .baseUrl(baseUrl1)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ArticleaaApi articleaaApi=retrofit1.create(ArticleaaApi.class);
    
        Call<ArticleBean> articleBeanCall = articleaaApi.getArticle();
        articleBeanCall.enqueue(new Callback<ArticleBean>() {
            @Override
            public void onResponse(Call<ArticleBean> call, Response<ArticleBean> response) {
                System.out.println("S");
            }
            @Override
            public void onFailure(Call<ArticleBean> call, Throwable t) {
                System.out.println("F");
            }
        });
    }
    

    API :

    public interface ArticleaaApi {
        @GET("wp-json/wp/v2/posts")
        Call<ArticleBean> getArticle();
    }
    
    Supplement 1  ·  Aug 31, 2016

    感谢各位,找到异常了:

    java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
    

    对于这种带数组的 json 数据该怎么用 Gson 解析呢? http://demo.wp-api.org/wp-json/wp/v2/posts

    Supplement 2  ·  Aug 31, 2016

    搞定了,谢谢大家! 有时候就是这样子,这么明显的错误自己就是没有看出来。

    public interface ArticleaaApi {
    @GET("wp-json/wp/v2/posts")
    Call<List<ArticleBean>> getArticle();
    }
    
    8 replies  •  2016-09-01 18:01:15 +08:00
    kamikat
    kamikat
    Aug 31, 2016
    1
    Log.e("F", t);
    sjn9588
    sjn9588
    Aug 31, 2016
    2
    t.getMessage()
    Override
    Override
    Aug 31, 2016
    3
    我仿佛又听到有人在背后偷偷 @我
    sunus
    sunus
    Aug 31, 2016
    4
    直接 attach retrofit 源码打断点呗
    baozijun
    baozijun
    Aug 31, 2016
    5
    gson 不能转换,可能是你的 bean 有问题,或者返回的信息解析成功,把 response 信息打印出来看一下。
    zarvin
    zarvin
    Aug 31, 2016
    6
    给 bean 套一个 list
    jinhan13789991
    jinhan13789991
    Aug 31, 2016
    7
    就正常的 List<Data> 啊, gson 有自动获取 type 树,然后转化的
    x9498
    x9498
    Sep 1, 2016
    8
    daibi
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2502 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 14:57 · PVG 22:57 · LAX 06:57 · JFK 09:57
    ♥ Do have faith in what you're doing.