Get JSON array from retrofit Response Get JSON array from retrofit Response arrays arrays

Get JSON array from retrofit Response


UPDATE FOR Retrofit 2.0-beta2:

dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.1.1'    compile 'com.google.code.gson:gson:2.4'    compile 'com.squareup.okhttp:okhttp:2.5.0'    // compile 'com.squareup.retrofit:retrofit:1.9.0'    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'}

Interface:

@GET("/api/values")Call<GetRoleData> getUserRoles();

MainActivity's onCreate:

        // Retrofit 2.0-beta2        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(API_URL_BASE)                .addConverterFactory(GsonConverterFactory.create())                .build();        WebAPIService service = retrofit.create(WebAPIService.class);        // Asynchronous Call in Retrofit 2.0-beta2        Call<GetRoleData> call = service.getUserRoles();        call.enqueue(new Callback<GetRoleData>() {            @Override            public void onResponse(Response<GetRoleData> response, Retrofit retrofit) {                ArrayList<GetRoleData.Roles> arrayList = response.body().getUserRoles();                if (arrayList != null) {                    Log.i(LOG_TAG, arrayList.get(0).getName());                }            }            @Override            public void onFailure(Throwable t) {                Log.e(LOG_TAG, t.toString());            }        });

Retrofit 1.9

I use your GetRoleData class

The interface:

public interface WebAPIService {            @GET("/api/values")    void getUserRoles(Callback<GetRoleData> callback);                       }

MainActivity:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                    // creating a RestAdapter using the custom client        RestAdapter restAdapter = new RestAdapter.Builder()                .setEndpoint(API_URL_BASE)                .setLogLevel(RestAdapter.LogLevel.FULL)                .setClient(new OkClient(mOkHttpClient))                .build();        WebAPIService webAPIService = restAdapter.create(WebAPIService.class);        Callback<GetRoleData> callback = new Callback<GetRoleData>() {            @Override            public void success(GetRoleData getRoleData, Response response) {                String bodyString = new String(((TypedByteArray) response.getBody()).getBytes());                Log.i(LOG_TAG, bodyString);            }            @Override            public void failure(RetrofitError error) {                String errorString = error.toString();                Log.e(LOG_TAG, errorString);            }        };        webAPIService.getUserRoles(callback);    }

The screenshot as the following:

enter image description here


Mocky for tests -> http://www.mocky.io/v2/567275072500008d0e995b2cI'm using Retrofit 2 (beta-2). This works for me, nothing special about it:

Call definition:

@GET("/v2/567275072500008d0e995b2c")Call<Base> getMock();

Models:

public class Base {    public int rc;    public String message;    public List<Role> he;}public class Role {    public String name;    public int type;}

Retrofit:

Retrofit retrofit = new Retrofit.Builder()                .baseUrl(baseUrl)                .addConverterFactory(GsonConverterFactory.create())                .build();

Call execute:

webservice.getMock().enqueue(new Callback<Base>() {    @Override    public void onResponse(Response<Base> response, Retrofit retrofit) {    }    @Override    public void onFailure(Throwable t) {    }});


You have written all your getters except he correctly. In order for Retrofit to parse your JSON file, you should write your getter for he variable as follows.

public ArrayList<Roles> getHe() {    return he;}

Also, try removing new ArrayList from the constructor.

public GetRoleData() {    // this.he = new ArrayList<>(); // <-- Remove here    this.message = "";}