这里的亲爱的朋友,正在寻找需要填充spinner的内容,这是来自rest服务使用翻新的JSON响应。经过长时间的研究,我找不到任何样本,任何人都可以基于这个共享样本,这对我非常有用!
这是JSON响应,请帮助我解析并填充微调器。
{
"d": [
{
"__type": "ServiceType:#TVS_Complaint_BL.Entities",
"Message": "OK",
"Success": true,
"ServiceCode": "P1",
"ServiceName": "Paid Service",
"nServiceID": 1
},
{
"__type": "ServiceType:#TVS_Complaint_BL.Entities",
"Message": "OK",
"Success": true,
"ServiceCode": "F1",
"ServiceName": "Free Service",
"nServiceID": 2
},
{
"__type": "ServiceType:#TVS_Complaint_BL.Entities",
"Message": "OK",
"Success": true,
"ServiceCode": "PS1",
"ServiceName": "Post Warranty",
"nServiceID": 3
},
{
"__type": "ServiceType:#TVS_Complaint_BL.Entities",
"Message": "OK",
"Success": true,
"ServiceCode": "FOC",
"ServiceName": "FOC",
"nServiceID": 4
}
]
}
车辆响应:
public class VehicleResponse {
private List<DBean> d;
public List<DBean> getD() {
return d;
}
public void setD(List<DBean> d) {
this.d = d;
}
public static class DBean {
/**
* __type : ServiceType:#TVS_Complaint_BL.Entities
* Message : OK
* Success : true
* ServiceCode : P1
* ServiceName : Paid Service
* nServiceID : 1
*/
private String __type;
private String Message;
private boolean Success;
private String ServiceCode;
private String ServiceName;
private int nServiceID;
public String get__type() {
return __type;
}
public void set__type(String __type) {
this.__type = __type;
}
public String getMessage() {
return Message;
}
public void setMessage(String Message) {
this.Message = Message;
}
public boolean isSuccess() {
return Success;
}
public void setSuccess(boolean Success) {
this.Success = Success;
}
public String getServiceCode() {
return ServiceCode;
}
public void setServiceCode(String ServiceCode) {
this.ServiceCode = ServiceCode;
}
public String getServiceName() {
return ServiceName;
}
public void setServiceName(String ServiceName) {
this.ServiceName = ServiceName;
}
public int getNServiceID() {
return nServiceID;
}
public void setNServiceID(int nServiceID) {
this.nServiceID = nServiceID;
}
}
}
Vechile请求:
public class VehicleRequest {
/**
* argRequest : {"Token":"356657bba5a94daeb02a2a5bd93c6b71"}
*/
private ArgRequestBean argRequest;
public ArgRequestBean getArgRequest() {
return argRequest;
}
public void setArgRequest(ArgRequestBean argRequest) {
this.argRequest = argRequest;
}
public static class ArgRequestBean {
/**
* Token : 356657bba5a94daeb02a2a5bd93c6b71
*/
private String Token;
public String getToken() {
return Token;
}
public void setToken(String Token) {
this.Token = Token;
}
}
}
答案 0 :(得分:0)
当您通过Retrofit使用API时,我建议您使用Gson。根据文档,以下依赖项应该起作用:
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
}
检索响应数据后,可以使用Gson到达DBean的每个实例。
假设您的响应数据以Response<JsonObject> response
的形式获取:
gson = new Gson();
List<DBean> dBeanList = new ArrayList<>();
JsonObject jsonObject = gson.toJsonTree(response.body()).getAsJsonObject();
dBeanList = jsonObject.getD().getAsJsonArray();
然后,您可以使用DBean
的列表填充微调器:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<DBean> adapterDBeans = new ArrayAdapter<>
(this,android.R.layout.simple_spinner_item, dBeanList);
adapterDBeans.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterDBeans);
参考:https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit