2016-11-28 159 views
-1

我正在實現一個使用Android的寧靜的客戶端。我有API URL,令牌等。我使用Retrofit 2庫來實現這個應用程序,都是正確的。但JSON值無法正確顯示。我嘗試了幾種方法來解決這個問題,但找不到任何正確的解決方案。

這是我的代碼:

JSON字符串

{"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}]} 

主要活動

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

} 

用戶類

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

} 

存款類

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

} 

接口類

public interface LoginApi { 

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


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

編輯...... 新存款類

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; 
}} 
+0

'getDeposits'是一個方法,一個陌生的名字,將返回'名單'或'User'。也許你應該重命名該類,以更明確你的意圖 –

+0

你的意思是重命名用戶類? – cpk

+0

這就是我的意思,是的。如果這是有道理的,我不知道。看起來像存款的對象,不一定是一個用戶,每一個說。無論如何,如果你可以控制服務器代碼,你可以將對象「扁平」爲一個列表。沒有必要使用'{「deposit」:'JSON部分,只需從'[{「id」:806638,「amount」:...' –

回答

2

我覺得你的API接口類應該是:

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

,並在你的活動:

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

      } 

    } 
+0

不,這是錯誤的方式,我直撥電話用戶類 – cpk

+0

@ChmathKalubovila這看起來是正確的。 –

+0

沒有傢伙,這不適合我 – cpk

相關問題