需要在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();
}
}
答案 0 :(得分:0)
您应该使用回调将响应从您的排球类传递到主要活动。请稍后再看看这篇SO帖子: How can I return value from function onResponse of Volley?