Как я могу использовать params для POST запроса и GET ответа? У меня есть два модельных класса. У каждого была телефонная переменная. Как я могу опубликовать без коллапса? как распечатать файл json
в logcat
?
Зависимости:
implementation 'com.mcxiaoke.volley:library:1.0.19'
implementation 'com.google.code.gson:gson:2.8.5'
Пользовательский Gson:
public class GsonRequest<T> extends Request<T> {
private Gson gson = new Gson();
private Class<T> clazz;
private Map<String, String> headers;
private Map<String, String> params;
private Listener<T> listener;
/**
* Make a GET request and return a parsed object from JSON.
*
* @param url URL of the request to make
* @param clazz Relevant class object, for Gson's reflection
* @param params Map of request headers
*/
public GsonRequest(int method, String url, Class<T> clazz, Map<String, String> params, Listener<T> listener, ErrorListener errorListener) {
super(method, url, errorListener);
this.clazz = clazz;
this.headers = null;
this.params = params;
this.listener = listener;
gson = new Gson();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers != null ? headers : super.getHeaders();
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
@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 | JsonSyntaxException e) {
return Response.error(new ParseError(e));
}
}
}
Запрос:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
GsonRequest<Model> gsonRequest = new GsonRequest<Model>(Request.Method.POST, url, Model.class, null, new Response.Listener<Model>() {
@Override
public void onResponse(Model response) {
Toast.makeText(getApplicationContext(), "success" + " " + response, Toast.LENGTH_LONG).show();
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText(error.toString());
Toast.makeText(getApplicationContext(), "error" + " " + error, Toast.LENGTH_LONG).show();
Log.d(TAG, error.toString());
}
});
requestQueue.add(gsonRequest);
}
Основная модель:
public class Model {
private Business business;
private Contacts contacts;
public Model(Business business, Contacts contacts) {
this.business = business;
this.contacts = contacts;
}
Бизнес-модель:
public class Business {
private String created_by;
private String business_name;
private String phone;
private String business_description;
private String address;
private String customer_name;
private String businessEmail;
private String business_type;
public Business(String created_by, String business_name, String phone, String business_description, String address,
String customer_name, String businessEmail, String business_type) {
this.created_by = created_by;
this.business_name = business_name;
this.phone = phone;
this.business_description = business_description;
this.address = address;
this.customer_name = customer_name;
this.businessEmail = businessEmail;
this.business_type = business_type;
}
Модель контакта:
public class Contacts {
private String lastName;
private String phone;
private String email;
private String firstName;
public Contacts(String lastName, String phone, String email, String firstName) {
this.lastName = lastName;
this.phone = phone;
this.email = email;
this.firstName = firstName;
}
Файл Json:
{ "business": {
"address": "xxx",
"business_description": "xx",
"businessEmail": "xxx@gmail.com",
"business_name": "xxx",
"phone":"1234567890",
"business_type": "xx",
"created_by": "xxx",
"customer_name": "xxx"
},
"contacts": [ {
"email": "yyy@gmail.com",
"firstName": "yyy",
"lastName": "yyy", "phone": "6544324569"
} ]
}