1

I am new to Android and I want your help regarding StringRequestVolley. In my project I will be calling several API's. For that I have created a method named ProcessAPIRequest. For this I will be passing URL as parameter. Here is my code snippet of function.

private static String iResponse="";

public static String ProcessAPIRequest(String pURL, Context pContext){
iResponse="";
StringRequest stringRequest = new StringRequest(pURL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String strResponse) {
                iResponse=strResponse;
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //error.networkResponse;
                //Log.e("error", "" + error);
            }
        });
RequestQueue requestQueue = Volley.newRequestQueue(pContext);
requestQueue.add(stringRequest);
return iResponse;
}

The problem I am facing is whenever I call this method I am getting empty Response. public void onResponse method is firing after the response is returned. I want response should be returned once it gets the result.

Sachin
  • 111
  • 1
  • 8
  • `onResponse(String strResponse)` has `void` as a return type hence you can not write `return` statement. Other way you can do is try calling another method like `manage_response(result)`. Pass the values to method and manage the result. – Suraj Aug 27 '18 at 12:36
  • Use Retrofit, please. – Obsthändler Aug 27 '18 at 12:43
  • Or you can execute your request synchronously, though bad approach (https://stackoverflow.com/questions/16904741/can-i-do-a-synchronous-request-with-volley) – Qasim Aug 27 '18 at 12:49

2 Answers2

1

You will use an callback interface for this. Here is an example.

public interface OnResponse{
   void onResponse(String response);
}

public static void ProcessAPIRequest(String pURL, Context pContext, final OnResponse onResponse){
StringRequest stringRequest = new StringRequest(pURL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String strResponse) {
                onResponse.onResponse(strResponse); // callback for success
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //error.networkResponse;
                //Log.e("error", "" + error);
                onResponse.onResponse(null); // callback for error
            }
        });
RequestQueue requestQueue = Volley.newRequestQueue(pContext);
requestQueue.add(stringRequest);
}
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
1

The problem is that the VolleyRequests run on Asynchronous threads , The right way to implement it is to have a callback once you get response .

create an interface

 public interface VolleyResultCallBack {

     void onVolleyResultListener(String response, String requestUrl);

     void onVolleyErrorListener(VolleyError error,String requestUrl);

 }

and your method

public static void ProcessAPIRequest(VolleyResultCallBack resultListener,String pURL, Context pContext){
iResponse="";
StringRequest stringRequest = new StringRequest(pURL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String strResponse) {
                resultListener.onVolleyResultListener(strResponse,pURL);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //error.networkResponse;
                //Log.e("error", "" + error);
              resultListener.onVolleyErrorListener(error,pURL);
            }
        });
RequestQueue requestQueue = Volley.newRequestQueue(pContext);
requestQueue.add(stringRequest);

}

Your Class:-

 public class TestActivity extends AppCompatActivity implements VolleyResultCallBack {

  @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

   ClassName.ProcessAPIRequest(this,"Your URl",this)

   }

 @Override
    public void onVolleyResultListener(String response, String requestUrl) {

     if(requestUrl.equals("Your URl"){
                //do some thing with response
          }    

    }

    @Override
    public void onVolleyErrorListener(VolleyError error,String requestUrl) {
        // error occured get error and show it
    }

}
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • Thank you @Redman. Your code solved half of my problem. Now the main problem is I will be calling many other APIs depending on the result of previous API result. In onVolleyResultListener method I may not be knowing which response I am having currently. Hope you got my problem. – Sachin Aug 27 '18 at 12:57
  • You will know it by using requestUrl – Manohar Aug 27 '18 at 15:27