G0D
V2EX  ›  Android

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

  •  
  •   G0D · Aug 31, 2016 · 16084 views
    This topic created in 3542 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
        1
    kamikat  
       Aug 31, 2016
    Log.e("F", t);
    sjn9588
        2
    sjn9588  
       Aug 31, 2016
    t.getMessage()
    Override
        3
    Override  
       Aug 31, 2016
    我仿佛又听到有人在背后偷偷 @我
    sunus
        4
    sunus  
       Aug 31, 2016
    直接 attach retrofit 源码打断点呗
    baozijun
        5
    baozijun  
       Aug 31, 2016
    gson 不能转换,可能是你的 bean 有问题,或者返回的信息解析成功,把 response 信息打印出来看一下。
    zarvin
        6
    zarvin  
       Aug 31, 2016
    给 bean 套一个 list
    jinhan13789991
        7
    jinhan13789991  
       Aug 31, 2016
    就正常的 List<Data> 啊, gson 有自动获取 type 树,然后转化的
    x9498
        8
    x9498  
       Sep 1, 2016
    daibi
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5848 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 121ms · UTC 01:58 · PVG 09:58 · LAX 18:58 · JFK 21:58
    ♥ Do have faith in what you're doing.