Maintaining session in android ( application stay authenticated on the server side)

后端 未结 5 1680
太阳男子
太阳男子 2020-11-29 16:29

I am building a login application in android in which i am hitting a url(with username and password) upto that part it works fine but after that whenever I am hitting a url(

5条回答
  •  猫巷女王i
    2020-11-29 17:15

    Here is an another implementation using Volley library ... a very useful hint from https://stackoverflow.com/a/36496607/3099185

        CustomRequest jsonObjReq = new CustomRequest(Request.Method.GET,
                url, null,  new Response.Listener() {
    
            @Override
            public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
            }
        }, new Response.ErrorListener(){
    
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
            }
        });
    

    Custom request class

    import android.util.Log;
    
    import com.android.volley.AuthFailureError;
    import com.android.volley.Response;
    import com.android.volley.toolbox.JsonObjectRequest;
    
    import org.json.JSONObject;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class CustomRequest extends JsonObjectRequest {
        private String session_id = "";
    
        public CustomRequest(int method, String url, JSONObject jsonRequest,
                                       Response.Listener listener, Response.ErrorListener errorListener) {
            super(method, url, jsonRequest, listener, errorListener);
        }
    
        public CustomRequest(int method, String url, JSONObject jsonRequest, String session_id,
                                       Response.Listener listener, Response.ErrorListener errorListener) {
            super(method, url, jsonRequest, listener, errorListener);
            this.session_id = session_id;
        }
    
    
        @Override
        public Map getHeaders() throws AuthFailureError {
            Map headers = new HashMap();
            Log.d(TAG, " -> session_id = " + session_id);
            if(!(session_id.equals(""))) {
                headers.put("Cookie", this.session_id);
            }
            return headers;
        }
    
    }
    

    Simple way of implementing volley using singleton pattern http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/

    Remember to initialize mRequestQueue in onCreate() to avoid unexpected null pointer exception

    @Override
    public void onCreate() {
        super.onCreate();
    
        // initialize the singleton
        sInstance = this;
            mRequestQueue = Volley.newRequestQueue(this);
    }
    

    Hope this help too ... ! :)

提交回复
热议问题