简体   繁体   中英

How to call API using Retrofit in Android

Link: https://mobileinterview.azurewebsites.net/api/products?PageIndex=1&PageSize=10

I am trying to call this API using retrofit in android using java but it does not return any response body it shows the response message is OK and the response code is 200, but still body is null.

Below is my code:

    retrofit = new Retrofit.Builder()
            .baseUrl(Constant.BaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

    MyApiService services = retrofit.create(MyApiService.class);

    Call<JSONArray> call = services.getAllData();

    call.enqueue(new Callback<JSONArray>() {
        @Override
        public void onResponse(Call<JSONArray> call, Response<JSONArray> response) {
            Log.i(TAG, "onResponse: "+response);
        }

        @Override
        public void onFailure(Call<JSONArray> call, Throwable t) {
            Log.i(TAG, "onFailure: "+t.getMessage());
        }
    });

Below is my interface class:

@GET("products?PageIndex=1&PageSize=10")
Call<JSONArray> getAllData();

your api data is xml based, you follow this topic for parsing the xml data, hopefully it will help your problem

https://codeleague.redquark.org/2018/08/parsing-xml-using-retrofit.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM