2017-07-03 44 views
-4

enter image description here無法使用改造

我試圖從下面的JSON得到temp_max的價值,但我得到null.Also得到迴應,我能得到鱈魚,消息和CNT的值。

我正在使用「」response.body()。getTempMax()「」來獲取值。讓我知道如果這不是正確的方式。

Model類

公共類主要{

@SerializedName("temp_min") 
@Expose 
private Double tempMin; 
@SerializedName("temp_max") 
@Expose 
private Double tempMax; 

public Double getTempMin() { 
    return tempMin; 
} 

public void setTempMin(Double tempMin) { 
    this.tempMin = tempMin; 
} 

public Double getTempMax() { 
    return tempMax; 
} 

public void setTempMax(Double tempMax) { 
    this.tempMax = tempMax; 
} 
} 

}

MainActivity

ApiInterface apiInterface = ApiClient.getClient()創建(ApiInterface.class)。

Call<Main> call = apiInterface.getWeather(); 
    call.enqueue(new Callback<Main>() 
    { 
     @Override 
     public void onResponse(Call<Main> call, Response<Main> response) 
     { 
      response.body(); 
      Toast.makeText(MainActivity.this, "The cnt is " + response.body().getTempMax(), Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onFailure(Call<Main> call, Throwable t) 
     { 
      Log.e("Main", t.toString()); 
     } 
    }); 
+0

你能粘貼代碼爲您解析類以及 – MRX

回答

0

你需要更新你提供解析

模型類
class JSONResponse{ 
    int cod; 
    String message; 
    int cnt; 
    ArrayList<Data> list; 
    } 


    class Data{ 
    long dt; 
    Main main; 

    } 

    class Main{ 
    //All properties related to main class 
    } 


This is how you need to structure. Isn't it simple ?. Now when you need to access members of Main class you need to traverse all the way from JsonResponse class to Data class and access Main object and access the property.