Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.86 KB | None | 0 0
  1. package br.com.gameloop.planbox.Utils;
  2.  
  3. import android.content.Context;
  4.  
  5. import com.android.volley.AuthFailureError;
  6. import com.android.volley.DefaultRetryPolicy;
  7. import com.android.volley.NetworkResponse;
  8. import com.android.volley.Request;
  9. import com.android.volley.RequestQueue;
  10. import com.android.volley.Response;
  11. import com.android.volley.ServerError;
  12. import com.android.volley.VolleyError;
  13. import com.android.volley.toolbox.HttpHeaderParser;
  14. import com.android.volley.toolbox.StringRequest;
  15. import com.android.volley.toolbox.Volley;
  16.  
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23.  
  24. import br.com.gameloop.planbox.classes.ResponseResult;
  25.  
  26. public class NetworkUtils {
  27.  
  28.     public static int REQUEST_TIMEOUT = 10000;
  29.     private RequestQueue requestQueue;
  30.     private ResponseResult responseResult;
  31.  
  32.     public NetworkUtils(Context context) {
  33.         requestQueue = Volley.newRequestQueue(context);
  34.     }
  35.  
  36.     public void doPost(final String url, final Map<String,String> params, final Callback callback) {
  37.  
  38.         StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
  39.                 new Response.Listener<String>() {
  40.  
  41.                     @Override
  42.                     public void onResponse(String response) {
  43.  
  44.                         ResponseResult responseResult;
  45.  
  46.                         try {
  47.  
  48.                             JSONObject jsonObject = new JSONObject(response);
  49.  
  50.                             if (jsonObject != null) {
  51.  
  52.                                 String message = "Requisição realizada com sucesso.";
  53.  
  54.                                 if(jsonObject.has("message")) {
  55.                                     message = jsonObject.getString("message");
  56.                                 }
  57.  
  58.                                 responseResult = new ResponseResult(jsonObject, message, true );
  59.                                 callback.onResult(responseResult);
  60.  
  61.                             } else {
  62.  
  63.                                 responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (001).", false );
  64.                                 callback.onResult(responseResult);
  65.                                
  66.                             }
  67.  
  68.                         } catch (Exception e) {
  69.  
  70.                             responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente. (002)", false );
  71.                             callback.onResult(responseResult);
  72.                            
  73.                         }
  74.  
  75.                     }
  76.  
  77.                 }, new Response.ErrorListener() {
  78.  
  79.             @Override
  80.             public void onErrorResponse(VolleyError error) {
  81.  
  82.                 NetworkResponse response = error.networkResponse;
  83.  
  84.                 if (error instanceof ServerError || error instanceof AuthFailureError && response != null) {
  85.  
  86.                     try {
  87.  
  88.                         String errorResponse = new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
  89.                         JSONObject jsonObject = new JSONObject(errorResponse);
  90.  
  91.                         String message = "Ocorreu um erro, por favor tente novamente. (003)";
  92.  
  93.                         if(jsonObject.has("message")) {
  94.                             message = jsonObject.getString("message");
  95.                         }
  96.  
  97.                         responseResult = new ResponseResult(jsonObject, message, true );
  98.                         callback.onResult(responseResult);
  99.  
  100.                     } catch (UnsupportedEncodingException e1) {
  101.  
  102.                         responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (004).", false );
  103.                         callback.onResult(responseResult);
  104.  
  105.                     } catch (JSONException e2) {
  106.  
  107.                         responseResult = new ResponseResult(null, "Ocorreu um erro, por favor tente novamente (005).", false );
  108.                         callback.onResult(responseResult);
  109.  
  110.                     }
  111.  
  112.                 }
  113.             }
  114.         }) {
  115.  
  116.             @Override
  117.             protected Map<String,String> getParams() {
  118.  
  119.                 return params;
  120.  
  121.             }
  122.  
  123.             @Override
  124.             public Map<String, String> getHeaders() throws AuthFailureError {
  125.  
  126.                 Map<String,String> params = new HashMap<String, String>();
  127.                 params.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
  128.                 return params;
  129.  
  130.             }
  131.  
  132.         };
  133.  
  134.         stringRequest.setRetryPolicy(new DefaultRetryPolicy(REQUEST_TIMEOUT,
  135.                 DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  136.                 DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  137.  
  138.         requestQueue.add(stringRequest);
  139.  
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement