我是一名初学者android开发人员,我想捕获id,uid等JSON数组响应。请帮我做到这一点,我从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中显示响应。
答案 0 :(得分:0)
首先,您在创建Context
对象时没有传递ApiInterface
。这就是为什么无法正确创建对象的原因。我应该在通过过程中通过Context
。
第二,您的Call<ResponseData>
中有ApiInterface
,在这里您尝试在List<>
中获取响应。因此您应该在Model Class对象中获取响应主体。
以此更新您的代码。
String data = "";
ApiInterface apiInterface = ApiClient.getInstance().getClient(MainActivity.this).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){
ResponseData data = response.body();
for(int i=0; i< data.getResponseData().size(); i++){
String id = data.getResponseData().getId();
String uid = data.getResponseData().getUid();
//Similarly you can get others
data = data + "Id= "+ id +", uId= "+ uid + "\n";
}
//setdetails to TextView
tvResponse.setText(data);
}
}
@Override
public void onFailure(Call<ResponseData> call, Throwable t) {
t.printStackTrace();
}
});
希望这对您有帮助!
答案 1 :(得分:0)
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();
tvResponse.setText(response.body().getResponseData().toString());
}
}
@Override
public void onFailure(Call<ResponseData> call, Throwable t) {
t.printStackTrace();
}
});
只需添加此行即可在textview中显示数据, 根据您当前的问题,如果您的问题不同,请详细说明
答案 2 :(得分:0)
问题是您在 getClient()方法中传递了 null 值。您没有获得客户。 使用以下代码:
public static Retrofit getClient() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.networkInterceptors().add(httpLoggingInterceptor);
builder.interceptors().add(httpLoggingInterceptor);
okHttpClient = builder.build();
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
// then call **getClient()** like below:
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();
}
});
答案 3 :(得分:0)
tvResponse.setText("show some text");
在您的示例中,如果要显示的项不止一个,您也可以使用append
。将其放在您的方法onResponse
中:
List<ResponseDataItem> data=response.body().getResponseData();
for (ResponseDataItem responseItem : data) {
tvResponse.append(responseItem.getId() + "\n");
tvResponse.append(responseItem.getUId() + "\n");
// append any other fields you want to show
}