我是一个初学者的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"
}
]
}
Api接口
public interface ApiInterface {
@GET("leave_dtls.php")
Call<ResponseData> getData();
}
Api客户端
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;
}
}
响应数据项
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;
}
}
主要活动
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中显示响应。
4条答案
按热度按时间rhfm7lfc1#
根据您当前的问题,添加这一行用于在文本视图中显示数据,如果您的问题不同,请详细说明
qyyhg6bp2#
问题是您在getClient()方法中传递了null值。您没有获取客户端。使用以下代码:
hc2pp10m3#
在示例中,如果要显示多个项,还可以使用
append
。将以下代码放入onResponse
方法中:yebdmbv44#
首先,你在创建
ApiInterface
对象时没有传递Context
。这就是为什么你的对象不能被正确创建的原因。你应该像我一样传递Context
。第二,你的
ApiInterface
中有Call<ResponseData>
,这里你试图获取List<>
中的响应,所以你应该在模型类对象中获得响应主体。用这个更新你的代码。
希望这对你有帮助!