没有pojo的改造和GSON

时间:2017-09-26 07:55:34

标签: java android gson retrofit jsonschema2pojo

我在app中使用黑客新闻api。我想使用gson factory转换器进行改装调用,但我无法将响应转换为pojo,因为它只是整数列表。

以下是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,15321070,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,15321002,15312103,15324530,15330981,15326045,15324917,15308824,15317471,15329527,15321015,15324386,15311280,15321772,15320778,15310849,15323267,15325456,15301766,15320489,15321031,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,15321069,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,15303210,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]

3 个答案:

答案 0 :(得分:4)

PoJo代表Plain Old Java Objects,如果你得到一个整数数组作为响应,在改造界面中解析它,

类似的东西:

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

期望整数数组或列表。

答案 1 :(得分:2)

首先创建这样的界面....

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

并在您的gradle中使用此depandencies ...

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

答案 2 :(得分:1)

请确保将响应包装在ResponseBody中,如下所示:

@GET(your url param)
Call<ResponseBody>getNews();

上面的Responsebody将返回响应,然后您可以使用下面的Statement使用此ResponseBody将其转换为JSONArray或JSONObject

JSONArray jsonArray=new JSONArray(ResponseBody.body().String())