vitordelfino

Volley metodo GET

May 13th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. /* Método utilizado para fazer requisições GET ao webservice retornando um objeto no formato Json*/
  2. /* Enquanto a requisição esta sendo processada, aparece um Dialog de "carregando"*/
  3.  
  4. public void get(Context contexto, String url, final VolleyCallbackObject callback){
  5.         final RequestQueue request = Volley.newRequestQueue(contexto);
  6.  
  7.         final JsonObjectRequest requisicao = new JsonObjectRequest(Request.Method.GET, url,
  8.                 new Response.Listener<JSONObject>(){
  9.  
  10.                     @Override
  11.                     public void onResponse(JSONObject response) {
  12.                         callback.onSuccess(response);
  13.                         dialog.dismiss();
  14.                     }
  15.                 }, new Response.ErrorListener(){
  16.  
  17.             @Override
  18.             public void onErrorResponse(VolleyError error ) {
  19.                 Log.i("ERROR", "onErrorResponse: " + error );
  20.                 dialog.dismiss();
  21.             }
  22.         });
  23.  
  24.         int socketTimeout = 30000;//30 seconds - change to what you want
  25.         RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
  26.         requisicao.setRetryPolicy(policy);
  27.  
  28.         request.add(requisicao);
  29.         dialog = new ProgressDialog(contexto);
  30.         dialog.setMessage("Carregando....");
  31.         dialog.show();
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment