我的預期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