我的预期JSON响应的Android与排球,GSON:如何定义类以下JSON响应
{
"first_name": "Rajesh",
"contacts": [
{
"id": "c200",
"name": "rajesh",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender": "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "ravi",
"email": "[email protected]",
"address": "xx-xx-xxxx,x - street, x - country",
"gender": "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
GsonRequest类进行申请并获得响应
public class GsonRequest<T> extends Request<T> {
private final Gson gson = new Gson();
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
public GsonRequest(String url, Class<T> clazz, Map<String, String> headers,
Listener<T> listener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.clazz = clazz;
this.headers = headers;
this.listener = listener;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected void deliverResponse(T response) {
listener.onResponse(response);
}
@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(gson.fromJson(json, clazz),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
我的问题是如何写类我可以通过GsonRequest类并获得回应 我试过http://www.jsonschema2pojo.org/为我的json响应生成类
现在我正在提出要求吨作为遵循
GsonRequest<MyClass> gsonRequest = new GsonRequest<MyClass>(url,MyClass.class, null,createMyReqSuccessListener(), createMyReqErrorListener());
我的回应侦听功能
private Response.Listener<MyClass> createMyReqSuccessListener() {
return new Response.Listener<MyClass>()
{
@Override
public void onResponse(MyClass response) {
txtView.setText("success" + response.first_name);
}
};
}
班JSON响应
public class MyClass {
public String first_name;
public List<Contact> contacts;
public class Contact {
public String id;
public String name;
public String email;
public String address;
public String gender;
public Phone phone;
}
public class Phone {
public String mobile;
public String home;
public String office;
}
}
请教,告诉我们如何提出请求和错误日志 – Yazazzello
请检查编辑问题 – RD1819
在“createMyReqSuccessListener()”中粘贴错误日志请求 – Yazazzello