Need to get the response in MainActivity
MyVolleyClass.java
public class MyVolleyClass {
Context context;
public String stringResponse()
{
StringRequest stringRequest=new StringRequest(Request.Method.POST, URLManager.BASE_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//need to return this response to my main activity
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getInstance(context).addTorequestque(stringRequest);
return ""; //whenever a response is got i need to return the response from the server to main activity
}
}
> MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//expecting the string returned from the MyVolleyClass.java
String response=new DataFetcherVolley(getApplicationContext()).dataFetch();
Toast.makeText(this,response,Toast.LENGTH_LONG).show();
}
}
You should use a callback to pass the response from your volley class to the main activity. Please take a look at this SO post from a while back : How can I return value from function onResponse of Volley?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.