我想通过单击导航抽屉中的新项目来调用截击。
如何从Fragment运行Volley?我可以在活动中轻松进行凌空抽射,但在“ this”中显示错误。IE
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
在上面的代码中如何在Fragment中运行凌空?在导航抽屉中?
我正在这样做:
public CollegesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_colleges, container, false);
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
Log.d("TAG", "hello");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
return rootView;
}
请给我个主意。
在返回rootView之前,您需要添加以下行:
queue.add(stringRequest);
您需要将请求添加到RequestQueue。
另外,Volley.newRequestQueue(context)
将上下文作为参数。因此,您应该传递活动上下文或应用程序上下文。
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句