2016-11-28 1 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

あなたはUserクラスの名前を変更することを意味しますか? – cpk

+0

それは私が意味したことです、はい。それが意味をなさないなら、私は知らない。私に預金の対象のように見えるが、必ずしもユーザではない。いずれにしても、サーバーコードを制御できれば、オブジェクトを単にリストにまとめることができます。 "{{" 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

関連する問題