2017-09-26 51 views
0

我在應用程序中使用黑客新聞api。我想使用gson工廠轉換器進行翻新調用,但是我無法將響應轉換爲pojo,因爲它只是整數列表。改裝和沒有pojo的GSON

下面是API調用和響應

https://hacker-news.firebaseio.com/v0/topstories.json

[15332819,15335462,15336072,15333899,15335010,15335146,15335216,15334678,15335039,15332171,15334533,15332613,15336632,15334763,15331016,15333082,15332051,15332394,15331604,15332122,15333838,15332187,15334207,15334381,15335717,15330541,15335107,15332770,15332753,15328356,15328247,15336492,15332099,15330564,15328744,15330712,15332138,15333036,15324954,15330757,15328889,15327551,15330864,15334158,15336389,15328516,15330409,15328698,15326480,15334931,15335520,15333085,15332145,15328861,15330556,15334665,15325649,15332306,15335032,15321850,15336063,15325691,15329356,15328850,15335962,15331999,15326098,15325154,15327837,15335448,15325767,15314006,15318440,15329550,15331089,15329374,15330830,15332929,15325226,15331750,15328165,15326335,15333198,15325820,15316175,15315129,15331393,15328948,15327502,15328680,15326652,15306398,15302730,15309393,15324818,15318530,15324596,15331731,15309989,70,15317075,15325659,15323707,15335129,15330835,15324883,15302484,15302707,15330966,15320936,15315813,15332031,15332370,15318904,15304242,15326984,15298833,15327455,15324011,15324542,15313257,15318233,15326845,15294171,15302841,15330977,15325480,15308771,15334514,15326648,15312755,15329699,15321555,15302500,15324547,15304482,15311773,15310465,15326553,15297912,15326276,15324656,15323842,15300069,15326158,15332620,15314486,15305892,15310664,15310291,15317275,15333631,15303254,15335753,15306365,15303833,15302662,15312966,15318741,15299849,02,15312103,15324530,15330981,15326045,15324917,15308824,15317471,15329527,15,15324386,15311280,15321772,15320778,15310849,15323267,15325456,15301766,15320489,31,15325545,15310680,15332367,15317639,15314262,15316726,15314510,15312303,15315028,15302887,15322805,15319616,15305774,15318958,15307205,15326577,15315863,15325735,15297861,15318390,15323042,15320623,15330052,15326106,15314877,15297915,15294742,15325829,15322887,15300052,15322627,15301982,15324195,15305991,15307358,15326535,15320922,15328992,15304139,15310154,15314629,15330970,15307563,15312118,15316627,15307152,15314663,15314964,15301151,15316762,15322415,15300241,15330355,15319340,15304951,15313330,15315844,15304269,15299900,15324237,15308577,15320952,15314492,15300407,15311274,15314659,15307870,15309190,15299359,15294131,15320342,15329781,15322447,15309929,15317515,15301131,15317735,15309290,15314020,15314503,15321807,15314058,15324325,15325188,15312288,15299165,15309403,15305581,15314276,15321588,15305258,15307097,15319237,15302186,15327658,15306523,15302718,15302701,15317788,15309249,15297371,15324414,15306223,15316719,15309683,15326666,15326089,15312132,15306976,15316348,15311742,15325030,15319103,15327571,15304863,15297708,15308667,15303894,15320426,15320676,15308680,15312046,15322156,15308022,15323136,15302035,15311952,15302636,15296170,15323754,15301638,15301785,15328618,15299822,15296438,15328266,15326165,15317902,15299023,15328481,15325457,15302432,15302592,15313787,15313307,15316994,15326297,15301981,15324838,15298050,15321750,15320682,15295146,15294722,15319061,15311955,15302495,15303947,15297827,15315213,15309072,15306920,15306278,15320799,15302317,15325546,15317835,15299754,15327183,15301848,15330037,15308858,15299116,15303555,15306187,15298570,15305476,15321366,15321323,15297448,15325264,15316148,15294483,15326021,15302749,69,15327575,15319476,15327225,15299138,15298300,15306612,15327253,15319758,15315008,15302750,15310829,15321162,15295561,15318821,15314229,15306193,15320616,15304210,15332726,15301365,15309228,15324169,15318644,15305042,15311711,15307009,15304935,15298146,15311954,15326684,15297164,15323962,,15301014,15295585,15299887,15307267,15307001,15293573,15297387,15314426,15304867,15293855,15307099,15293596,15310588,15307180,15303079,15324055,15314077,15318880,15306846,15306593,15317078,15326176,15296245,15295992,15297545,15320880,15309332,15295813,15317987,15314741,15325563,15313733,15306990,15317376,15308043,15326456,15306295,15294929,15293902,15298337,15295228,15311889,15300357,15293605,15314046,15300142,15305035,15297770,15293741,15299442,15302464,15314615,15302251,15309767,15302857,15298862,15295834,15301591,15314781,15301351,15304550,15311658,15307987,15304485,15332474,15307383,15320350,15299205,15307054,15323574,15332566,15305762,15319775,15306074,15319474,15297748,15304907,15305220,15317438,15320809,15303835,15307014,15317109,15320767,15305674,15300269,15316517,15328743,15312652,15300688,15298881,15318128,15313280,15321945,15313153,15306169,15316935,15318889,15304986,15307778,15330453] 
+0

你面臨的問題是什麼? – sushildlh

+0

無法爲gson創建pojo – apk

+1

您不想創建任何pojo。直接使用'ArrayList '; – sushildlh

回答

1

首先創建這樣的接口....

public interface GitHubService { 
     @GET("v0/topstories.json") 
     Call<List<Long>> listRepos(); 
    } 

並在活動像這樣使用.....

 Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("https://hacker-news.firebaseio.com/").addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

     GitHubService api = retrofit.create(GitHubService.class); 

     api.listRepos().enqueue(new Callback<List<Long>>() { 
      @Override 
      public void onResponse(Call<List<Long>> call, Response<List<Long>> response) { 
       if (response.isSuccessful()) { 

        // do operations here 
        String number = ""; 
        for (int i = 0; i < response.body().size(); i++) 
         number += response.body().get(i) + ", "; 

        ((TextView) findViewById(R.id.number)).setText(number); 
       } else 
        Toast.makeText(MainActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show(); 
      } 

      @Override 
      public void onFailure(Call<List<Long>> call, Throwable t) { 
      Toast.makeText(MainActivity.this, t.getMessage().toString(), Toast.LENGTH_LONG).show(); 
      } 
     }); 

並使用該depandencies在gradle ...

> compile 'com.squareup.retrofit2:retrofit:2.3.0' 
>  compile 'com.squareup.retrofit2:converter-gson:2.+' 
3

POJO代表簡單Java對象,如果你得到一個整數數組作爲響應,在更新接口解析它,

是這樣的:

@GET(... you url) 
Call<List<Integer>>getHackerNews() 

期望一個整數數組或列表。