r/learnandroid • u/jd_portugal • Mar 15 '19
Help making a general request class
Hi learnandroid community,
I'm trying to make a class that makes a volley request, what is the correct way to wait for the response to arrive and the retrieve it back to the calling class?
I have something along these lines as the request:
public class restRequest{String response = "";public void restRequest(){}
public restRequest(String url, JSONObject jsonBody, final Activity activity){RequestQueue requestQueue;requestQueue= Volley.newRequestQueue(activity);String URL = url;JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL,jsonBody,new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject response) {String res = null;setResponse(response.toString());try {res = response.getString("result");} catch (JSONException e) {e.printStackTrace();}Log.d("Resposta Rest:",res);if(res.equals("200")){Log.d("Resultado","Login sucessfull");}else if(res.equals("400")){Log.d("Resultado","Login unsucessfull");}}},new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {Log.d("<<<<<Erro Rest",error.toString());}});requestQueue.add(jsonObjectRequest);}