123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- package com.luticate.utils.business;
-
- import android.content.Context;
- import android.net.Uri;
-
- import com.android.volley.AuthFailureError;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import com.luticate.utils.dbo.LuDataAccessConfigDbo;
- import com.luticate.utils.dbo.LuDbo;
-
- import org.json.JSONObject;
-
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- *
- * Created by robin on 11/27/15.
- */
- public class LuRequest {
-
- private static RequestQueue _requestQueue = null;
-
- public static void init(Context ctx)
- {
- _requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
- }
-
- protected static <T extends LuDbo> Response.Listener<String> getListener(final Class<T> type, final LuPromise<T> promise)
- {
- return new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- try {
- T dbo = type.newInstance();
- try {
- JSONObject json = new JSONObject(response);
- JSONObject obj = null;
- if (!json.isNull("data")) {
- obj = json.optJSONObject("data");
- if (obj == null) {
- obj = new JSONObject();
- obj.put("value", json.opt("data"));
- }
- }
- dbo.fromJson(obj);
- promise.resolve(dbo);
- } catch (Exception e)
- {
- e.printStackTrace();
- promise.reject(new LuPromise.LuPromiseError("Failed to parse success server response", 200));
- }
- } catch (Exception e) {
- e.printStackTrace();
- promise.reject(new LuPromise.LuPromiseError("Failed to initialize server response", 200));
- }
- }
- };
- }
-
- protected static <T extends LuDbo> Response.ErrorListener getErrorListener(final LuPromise<T> promise)
- {
- return new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- int code = 0;
- try
- {
- if (error != null) {
- if (error.networkResponse != null)
- {
- code = error.networkResponse.statusCode;
- JSONObject data = new JSONObject(new String(error.networkResponse.data));
- promise.reject(new LuPromise.LuPromiseError(data.getString("Message"), code));
- }
- else {
- if (error.getCause() != null) {
- promise.reject(new LuPromise.LuPromiseError(error.getCause().getMessage(), code));
- }
- else {
- promise.reject(new LuPromise.LuPromiseError(error.toString(), code));
- }
- }
- }
- else {
- promise.reject(new LuPromise.LuPromiseError("Unknown network error", code));
- }
- } catch (Exception e)
- {
- promise.reject(new LuPromise.LuPromiseError(e.getMessage(), code));
- }
- }
- };
- }
-
- protected static <T extends LuDbo> LuPromise<T> request(final LuDataAccessConfigDbo config,
- int method, Class<T> type, String url,
- final HashMap<String, String> params)
- {
- LuPromise<T> promise = new LuPromise<>();
- StringRequest request = new StringRequest(method, config.getBaseUrl() + url,
- getListener(type, promise), getErrorListener(promise))
- {
- @Override
- public String getBodyContentType() {
- return "application/x-www-form-urlencoded; charset=UTF-8";
- }
-
- @Override
- protected Map<String, String> getParams() {
- return params;
- }
-
- @Override
- public Map<String, String> getHeaders() {
- return config.getHeaders();
- }
- };
- _requestQueue.add(request);
- return promise;
- }
-
- public static <T extends LuDbo> LuPromise<T> get(final LuDataAccessConfigDbo config, Class<T> type,
- String url, final HashMap<String, String> getParams)
- {
- Uri.Builder uri = Uri.parse(url).buildUpon();
- for (String key : getParams.keySet())
- {
- uri.appendQueryParameter(key, getParams.get(key));
- }
- LuPromise<T> promise = new LuPromise<>();
- StringRequest request = new StringRequest(Request.Method.GET, config.getBaseUrl() + uri.toString(),
- getListener(type, promise), getErrorListener(promise))
- {
- @Override
- public Map<String, String> getHeaders() {
- return config.getHeaders();
- }
- };
- _requestQueue.add(request);
- return promise;
- }
-
- public static <T extends LuDbo> LuPromise<T> get(LuDataAccessConfigDbo config, Class<T> type,
- String url)
- {
- return get(config, type, url, new HashMap<String, String>());
- }
-
- public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
- String url, final HashMap<String, String> postParams)
- {
- return request(config, Request.Method.POST, type, url, postParams);
- }
-
- public static <T extends LuDbo> LuPromise<T> post(LuDataAccessConfigDbo config, Class<T> type,
- String url)
- {
- return post(config, type, url, new HashMap<String, String>());
- }
- }
|