如何在Volley的onResponse上打开新活动

如何在Volley的onResponse上打开新活动

本文介绍了如何在Volley的onResponse上打开新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在Volley的onResponse上打开一个新活动.但是我不知道可以使用哪些参数.确实,我的Volley请求是在另一堂课上,我想我需要在第二堂课上传递我的第一堂课的内容.但是我不知道该怎么办.

I try to open a new activity on a onResponse of Volley. But I have no idea of what parameters I can use.Indeed my Volley request is on another class and, I think I need to pass the context of my first class on the second. But I have no idea of how I can do.

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请求的功能的一部分:

And that is a part of my function that do the request 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);

}

推荐答案

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(登录名,密码); 使用 req.TokenRequest(ConnexionActivity.this,登录名和密码);

and in your activity change req.TokenRequest(login, password);with req.TokenRequest(ConnexionActivity.this,login, password);

这篇关于如何在Volley的onResponse上打开新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:14