Reputation: 111
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.
Upvotes: 1
Views: 112
Reputation: 23394
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
}
}
Upvotes: 1
Reputation: 58974
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);
}
Upvotes: 1