我尝试在Volley的onResponse上打开一个新活动。但我不知道我可以使用哪些参数。 确实,我的Volley请求是在另一个班级,我想我需要在第二节课上传递我的第一堂课的背景。但我不知道我该怎么做。
public class ConnexionActivity extends Activity{
EditText textlogin;
EditText textpassword;
Button btnConnexion;
String login;
String password;
private AllRequest req;
private PrefManager pref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connexion);
this.req = new AllRequest(getApplicationContext());
this.pref = new PrefManager(getApplicationContext());
textlogin = (EditText) findViewById(R.id.editText_login_connexion);
textpassword = (EditText) findViewById(R.id.editText_mdp_connexion);
btnConnexion = (Button) findViewById(R.id.btn_Connexion);
btnConnexion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login = textlogin.getText().toString();
password = textpassword.getText().toString();
req.TokenRequest(login, password);
}
});
}
}
这是我的功能的一部分,请求Volley:
public void TokenRequest(final String login, final String password){
final StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, ressources.urlToken,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("onResponse", response);
pref = new PrefManager(context);
pref.storeIsConnect(true);
try{
JSONObject rep = new JSONObject(response);
//stockage des données
pref.storeScope(rep.getString("scope"));
pref.storeTokenType(rep.getString("token_type"));
pref.storeAccessToken(rep.getString("access_token"));
pref.storeRefreshToken(rep.getString("refresh_token"));
pref.storeExpiresIn(rep.getString("expires_in"));
}catch (JSONException e) {
e.printStackTrace();
Log.e("erreurJSON", e.getMessage());
}
//I don't know how do this part
Intent intent = new Intent(ConnexionActivity.getContext(), listMirorActivity.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", error.toString());
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", login);
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
// add headers <key,value>
String credentials = ressources.client_id + ":" + ressources.client_secret;
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
headers.put("Authorization", auth);
return headers;
}
};
queue.add(stringRequest);
}
答案 0 :(得分:2)
public void TokenRequest(final Context context,final String login, final String password){
final StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.POST, ressources.urlToken,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("onResponse", response);
pref = new PrefManager(context);
pref.storeIsConnect(true);
try{
JSONObject rep = new JSONObject(response);
//stockage des données
pref.storeScope(rep.getString("scope"));
pref.storeTokenType(rep.getString("token_type"));
pref.storeAccessToken(rep.getString("access_token"));
pref.storeRefreshToken(rep.getString("refresh_token"));
pref.storeExpiresIn(rep.getString("expires_in"));
}catch (JSONException e) {
e.printStackTrace();
Log.e("erreurJSON", e.getMessage());
}
//I don't know how do this part
Intent intent = new Intent(context, listMirorActivity.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse", error.toString());
}
}){
@Override
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", login);
params.put("password", password);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
// add headers <key,value>
String credentials = ressources.client_id + ":" + ressources.client_secret;
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
headers.put("Authorization", auth);
return headers;
}
};
queue.add(stringRequest);
}
并在您的活动中更改req.TokenRequest(login, password);
与req.TokenRequest(ConnexionActivity.this,login, password);
答案 1 :(得分:0)
您还可以创建一个Context变量Context context;
然后在调用OnCreate()时初始化它
context=this;
所以,当意图正在进行时,请执行此操作
Intent intent = new Intent(context, listMirorActivity.class);
startActivity(intent);