Я начинающий разработчик Android. Я хочу узнать ответ массива JSON, такой как id, uid и т. Д. Пожалуйста, помогите мне сделать это. Я получил много помощи от YouTube, но решения не существует.Я застрял с этим.Я даю все файлы, которые я создал для этого, а также JSON.
JSON-файл
{
"status": 0,
"response_data": [
{
"id": "12",
"uid": "USER00000003",
"reason": "Test",
"type": "Plan Leave",
"SataDate": "2018-09-18",
"EndDate": "2018-09-25",
"ApprovedBy": "USER00000002",
"ApprovedDate": "2018-09-18",
"Status": "REJECTED",
"Remarks": "Test Reject"
},
{
"id": "13",
"uid": "USER00000003",
"reason": "Wedding",
"type": "Plan Leave",
"SataDate": "2018-01-28",
"EndDate": "2018-02-05",
"ApprovedBy": "USER00000002",
"ApprovedDate": "2018-09-18",
"Status": "APPROVED",
"Remarks": "Ok"
}
]
}
ApiInterface
public interface ApiInterface {
@GET("leave_dtls.php")
Call<ResponseData> getData();
}
ApiClient
public class ApiClient {
private final static String BASE_URL = "http://api.xxxx.com/app/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
Данные ответа
public class ResponseData {
@SerializedName("response_data")
private List<ResponseDataItem> responseData;
@SerializedName("status")
private int status;
public void setResponseData(List<ResponseDataItem> responseData){
this.responseData = responseData;
}
public List<ResponseDataItem> getResponseData(){
return responseData;
}
public void setStatus(int status){
this.status = status;
}
public int getStatus(){
return status;
}
}
ResponseDataItem
public class ResponseDataItem {
@SerializedName("Status")
private String status;
@SerializedName("uid")
private String uid;
@SerializedName("reason")
private String reason;
@SerializedName("ApprovedDate")
private String approvedDate;
@SerializedName("Remarks")
private String remarks;
@SerializedName("ApprovedBy")
private String approvedBy;
@SerializedName("id")
private String id;
@SerializedName("type")
private String type;
@SerializedName("EndDate")
private String endDate;
@SerializedName("SataDate")
private String sataDate;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getApprovedDate() {
return approvedDate;
}
public void setApprovedDate(String approvedDate) {
this.approvedDate = approvedDate;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getApprovedBy() {
return approvedBy;
}
public void setApprovedBy(String approvedBy) {
this.approvedBy = approvedBy;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getSataDate() {
return sataDate;
}
public void setSataDate(String sataDate) {
this.sataDate = sataDate;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
TextView tvResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResponse=findViewById(R.id.tvResponse);
ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
Call<ResponseData> responseDataCall=apiInterface.getData();
responseDataCall.enqueue(new Callback<ResponseData>() {
@Override
public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
if (response.isSuccessful() && response.body()!=null && response!=null){
List<ResponseDataItem> data=response.body().getResponseData();
}
}
@Override
public void onFailure(Call<ResponseData> call, Throwable t) {
t.printStackTrace();
}
});
}
}
просто хочу показать ответ в TEXTVIEW.