Updated on 2026-08-14
This commit is contained in:
parent
d6e56d69d5
commit
7130b72624
6 changed files with 155 additions and 35 deletions
98
app/src/main/java/com/tangem/data/network/GsonRequest.java
Normal file
98
app/src/main/java/com/tangem/data/network/GsonRequest.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package com.tangem.data.network;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.AuthFailureError;
|
||||
import com.android.volley.NetworkResponse;
|
||||
import com.android.volley.ParseError;
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.Response.ErrorListener;
|
||||
import com.android.volley.Response.Listener;
|
||||
import com.android.volley.toolbox.HttpHeaderParser;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Volley adapter for JSON requests with POST method that will be parsed into Java objects by Gson.
|
||||
*/
|
||||
public class GsonRequest<T> extends Request<T> {
|
||||
public static final String TAG = "api." + GsonRequest.class.getSimpleName();
|
||||
private Gson mGson = 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
|
||||
*/
|
||||
public GsonRequest(int method,
|
||||
String url,
|
||||
Class<T> clazz,
|
||||
Listener<T> listener,
|
||||
ErrorListener errorListener) {
|
||||
super(method, url, errorListener);
|
||||
this.clazz = clazz;
|
||||
this.listener = listener;
|
||||
mGson = new Gson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a POST 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
|
||||
*/
|
||||
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.params = params;
|
||||
this.listener = listener;
|
||||
this.headers = null;
|
||||
mGson = 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));
|
||||
Log.i(TAG, (json));
|
||||
return Response.success(mGson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
Log.e(TAG, "UnsupportedEncodingException");
|
||||
return Response.error(new ParseError(e));
|
||||
} catch (JsonSyntaxException e) {
|
||||
Log.e(TAG, "JsonSyntaxException");
|
||||
return Response.error(new ParseError(e));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -3,18 +3,21 @@ package com.tangem.data.network
|
|||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.util.Log
|
||||
import com.android.volley.AuthFailureError
|
||||
import com.android.volley.DefaultRetryPolicy
|
||||
import com.android.volley.Request
|
||||
import com.android.volley.Response
|
||||
import com.android.volley.toolbox.StringRequest
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.JsonParser
|
||||
import com.tangem.AppController
|
||||
import com.tangem.presentation.fragment.LoadedWallet
|
||||
import com.tangem.wallet.R
|
||||
|
||||
class VolleyHelper {
|
||||
|
||||
fun doRequest(context: Context, url: String, params: Map<String, String>) {
|
||||
fun doRequestDebug(context: Context, url: String, params: Map<String, String>) {
|
||||
val stringRequest = object : StringRequest(Request.Method.POST, url,
|
||||
{ response ->
|
||||
val data = GsonBuilder().setPrettyPrinting().create().toJson(JsonParser().parse(response))
|
||||
|
|
@ -51,4 +54,28 @@ class VolleyHelper {
|
|||
AppController.getInstance().addToRequestQueue(stringRequest)
|
||||
}
|
||||
|
||||
fun doRequestString(context: Context, url: String, params: Map<String, String>) {
|
||||
val stringRequest = object : StringRequest(Request.Method.POST, url,
|
||||
Response.Listener<String> { response ->
|
||||
// val json = String(response)
|
||||
Log.i(LoadedWallet.TAG, response)
|
||||
},
|
||||
Response.ErrorListener { "That didn't work!" }) {
|
||||
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getHeaders(): Map<String, String> {
|
||||
return Server.getHeader()
|
||||
}
|
||||
|
||||
@Throws(AuthFailureError::class)
|
||||
override fun getParams(): Map<String, String> {
|
||||
return params
|
||||
}
|
||||
}
|
||||
|
||||
stringRequest.retryPolicy = DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)
|
||||
AppController.getInstance().addToRequestQueue(stringRequest)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue