How to get List from JSON list using Retrofit 2?

前端 未结 2 1438
再見小時候
再見小時候 2021-01-26 20:21

I am new to Android. I need to get a list of strings from a JSON list. It will be added to a Spinner list. I have the list on server like

[{\"bgrou         


        
2条回答
  •  星月不相逢
    2021-01-26 21:08

    Make the following model class (parcelable)

    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example implements Parcelable {
    
    @SerializedName("bgroup")
    @Expose
    private String bgroup;
    
    public String getBgroup() {
    return bgroup;
    }
    
    public void setBgroup(String bgroup) {
    this.bgroup = bgroup;
    }
    
    
        protected Example(Parcel in) {
            bgroup = in.readString();
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(bgroup);
        }
    
        @SuppressWarnings("unused")
        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
            @Override
            public Example createFromParcel(Parcel in) {
                return new Example(in);
            }
    
            @Override
            public Example[] newArray(int size) {
                return new Example[size];
            }
        };
    }
    

    Than create interface class like this

    public interface ApiService {
    
    @GET("")
    Call> getBloodGroups();
    }
    

    And finally call retrofit like following :

    Call> call = new RestClient(this).getApiService()
                    .getBloodGroups();
            call.enqueue(new Callback>() {
                @Override
                public void onResponse(Call> call, Response> response) {
    
                }
    
                @Override
                public void onFailure(Call> call, Throwable throwable) {
    
                }
            });
    

提交回复
热议问题