-1

I'm implementing a restful client using Android. I have API URL, token, etc. I implement this application using Retrofit 2 library, all are correct. But the JSON values do not display properly. I've tried several ways to solved this problem but can't find any correct solution for this.

This is my code:

JSON string

{"deposits":[{"id":806638,"amount":100,"status":"Canceled","reason":null},{"id":814436,"amount":1,"status":"Approved","reason":null},{"id":814452,"amount":1,"status":"Approved","reason":null},{"id":814505,"amount":1,"status":"Approved","reason":null}]}

Main Activity

void getRetrofitArray() {

    final String API_BASE_URL = "https://www.example.com/";
    final String credentials = "Bearer dfdfdfdlcdvksbdjsbdlvffdfddfdfdfjloiulfjuktj92p0JTFwJuFHlObIlWn8feQ_WA";
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new okhttp3.Interceptor() {
                @Override
                public Response intercept(okhttp3.Interceptor.Chain chain) throws IOException {

                    Request request = chain.request().newBuilder()
                            .addHeader("Authorization", credentials)
                            .addHeader("Accept", "application/json").build();

                    return chain.proceed(request);

                }
            }).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .build();

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


    LoginApi service = retrofit.create(LoginApi.class);



    Call <List <Users>>call = service.getDeposits();
    Log.d("onResponse", "There is an xlllllllllllllllllllllkkkkkkklll");
    call.enqueue(new Callback<List<Users>>(){
        @Override
        public void onResponse(Call<List<Users>>call, retrofit2.Response<List<Users>>response) {

            try {

              // Users   UserData = response.body();
                List <Users> UserData =response.body();

                //String jsonString= response.body().toString();
               // Type listType = new TypeToken<List<Users>>(){}.getType();

                                      for (int i = 0; i < UserData.size(); i++) {

                     text_marks_1.setText("StudentMarks  : " +UserData.get(i).getDeposits());


                   // else if (i == 2) {
                        //text_id_2.setText("StudentId  :  " + UserData.get(i).getName());
                   //
               }
            } catch (Exception e) {
                Log.d("onResponse", "There is an error");
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<List<Users>>call, Throwable t) {
            Log.d("onResponse", "There is an error");
            t.printStackTrace();
        }
    });

}

Users Class

public class Users {
    @SerializedName("deposits")
    @Expose
    private List<Deposit> deposits = new ArrayList<Deposit>();

    /**
     *
     * @return
     * The deposits
     *
     */
    public List<Deposit> getDeposits() {
        return deposits;
    }

    /**
     *
     * @param deposits
     * The deposits
     */
    public void setDeposits(List<Deposit> deposits) {
        this.deposits = deposits;
    }

}

Deposits Class

public class Deposit {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("amount")

    @Expose
    private Integer amount;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("reason")
    @Expose
    private Object reason;

    /**
     *
     * @return
     * The id
     */
    public Integer getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The amount
     */
    public Integer getAmount() {
        return amount;
    }

    /**
     *
     * @param amount
     * The amount
     */
    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    /**
     *
     * @return
     * The status
     */
    public String getStatus() {
        return status;
    }

    /**
     *
     * @param status
     * The status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     *
     * @return
     * The reason
     */
    public Object getReason() {
        return reason;
    }

    /**
     *
     * @param reason
     * The reason
     */
    public void setReason(Object reason) {
        this.reason = reason;
    }

}

interface class

public interface LoginApi {

    //@GET("/media/webservice/JsonReturn.php HTTP/1.1")
    // Call<List<User>> getUserDetails();


    @GET("api/deposits")
    Call <List<Users>> getDeposits();
}

edited...... new Deposit class

public class Deposit implements Serializable {

@SerializedName("deposits")
@Expose
private List<Deposit> deposits ;   

public List<Deposit> getDeposits() {
    return deposits;
}

public void setDeposits(List<Deposit> deposits) {
    this.deposits = deposits;
}

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("amount")
@Expose
private Integer amount;
@SerializedName("status")
@Expose
private String status;
@SerializedName("reason")
@Expose
private Object reason;

public Integer getId() {
    return id;
}


public void setId(Integer id) {
    this.id = id;
}


public Integer getAmount() {
    return amount;
}


public void setAmount(Integer amount) {
    this.amount = amount;
}


public String getStatus() {
    return status;
}


public void setStatus(String status) {
    this.status = status;
}

public Object getReason() {
    return reason;
}


public void setReason(Object reason) {
    this.reason = reason;
}}
cpk
  • 17
  • 1
  • 9
  • `getDeposits` is a strange name for a method that would return `List` or `User`. Maybe you should rename that class to be more clear on your intent – OneCricketeer Nov 28 '16 at 04:34
  • you mean rename the User class? – cpk Nov 28 '16 at 05:16
  • That's what I meant, yes. If that makes sense, I do not know. Looks like an object of deposits to me, not necessarily a User, per-say. In any case, if you have control over the server code, you could "flatten" the object into just a list. No need for the `{"deposits":` piece of the JSON, just start with `[{"id":806638,"amount":...` – OneCricketeer Nov 28 '16 at 05:19
  • @criket_007 it's not working – cpk Nov 28 '16 at 06:35
  • I have no idea what you changed. Just look at your JSON, though. You have a response of `{"deposits": `, yes? This is **not** a `List` at all, it is an object with a field of a `List deposits;`... This appears to be your `Users` class. Therefore, a `Call` is correct. If you see otherwise, and are getting different errors, please clarify those with an [edit] to your question. – OneCricketeer Nov 28 '16 at 06:42
  • @cricket_007 it's working..thanks for your supporting i create a one pojo class and call it. thank you very much.. – cpk Nov 28 '16 at 11:30

1 Answers1

2

i think your api interface class should be :

@GET("api/deposits")
Call<User> getDeposits();

and in your Activity :

Call<User> call = service.getDeposits();
call.enqueue(new Callback<User>(){
    @Override
    public void onResponse(Call<User>call, retrofit2.Response<User>response) {

            List<Deposit> = response.getDeposits();

           }

    }
boni octavianus
  • 286
  • 3
  • 6