java 如何在 Volley 中创建一个新的 newRequestQueue,Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32795970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a new newRequestQueue In Volley, Android
提问by Bryan
I have an fragment where I try to Instantiate a new newRequestQueue
with the Volley API.
我有一个片段,我尝试newRequestQueue
使用 Volley API实例化一个新片段。
I try to instantiate it like this:
我尝试像这样实例化它:
RequestQueue queue = Volley.newRequestQueue(this);
However, when I try to create the request, I get the following error:
但是,当我尝试创建请求时,出现以下错误:
newRequestQueue In Volley cannot be applied to annonymous android.view.View.OnClickListener
Here is my Fragment class:
这是我的片段类:
public class LoginFragment extends Fragment {
private FacebookLogin fLogin;
private Profile profile;
private CallbackManager mCallbackManager; //Used in activity result below. Gets a value when u hit the button
private static final String TAG = "Event";
private static String url_create_user = "127.0.0.1/create_row.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fLogin = new FacebookLogin(getActivity().getApplicationContext());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.login, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
final LoginButton loginButton = (LoginButton) getView().findViewById(R.id.login_button);
final TextView infoText = (TextView) getView().findViewById(R.id.text_details);
final ImageView profilP = (ImageView) getView().findViewById(R.id.profilePicture);
loginButton.setFragment(this);
loginButton.setReadPermissions("user_friends");
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, " Button clickde");
fLogin.setCallback(loginButton); //Let's register a callback
RequestQueue queue = Volley.newRequestQueue(this);
fLogin.setFacebookListener(new FacebookLogin.OnFacebookListener() {
@Override
public void onFacebookLoggedIn(JSONObject parameters) {
Log.i(TAG, "INNNNNNNNNNNE");
Log.i(TAG, parameters.toString());
}
});
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager = fLogin.getCallbackManager(); //Get the callbackmanager
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
回答by Blackbelt
the parameter of newRequestQueue
is a Context object. In your case, this, refers to the View.OnClickListener
anonymous inner class, where you are calling newRequestQueue
. Change
的参数newRequestQueue
是一个 Context 对象。在您的情况下, this 指的是View.OnClickListener
匿名内部类,您在其中调用 newRequestQueue
. 改变
Volley.newRequestQueue(this);
with
和
Volley.newRequestQueue(getActivity().getApplicationContext());
is, of course, getActivity()
because you are subclassing Fragment
是,当然,getActivity()
因为你是子类化Fragment
回答by Raphael
RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);
In this case, SignupActivity
is the activity you have typed your code in.
在这种情况下,SignupActivity
是您输入代码的活动。