美文网首页
Retrofit 2.0.1 获取请求头部

Retrofit 2.0.1 获取请求头部

作者: 小张呵呵 | 来源:发表于2016-04-14 12:25 被阅读1334次

    如何添加 获取 请求的 Cookie
    1.rest

     @FormUrlEncoded 
     @POST("api/user/login")
     Call<RequestResult> login(  
            @Field("account") String account,
            @Field("password") String password );
    

    2.使用

        Call<RequestResult<User>> call =  APIManager.getREST().login(account, password);
        Call<RequestResult<User>> call2 = call.clone();
        call2.enqueue(new Callback<RequestResult<User>>() {
                    @Override
                    public void onResponse(Call<RequestResult<User>> call,    Response<RequestResult<User>> response) {
                      
                        //获取cookie
                        String sessionId = userHelper.getSessionCookie(response.headers().get("Set-Cookie"));
                        //保存cookie
                       
                        //跳转主页面
                        Intent intent = new Intent(activity, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        activity.startActivity(intent);
                        //记得加这句话
                        finish();
                    }
    
                    @Override
                    public void onFailure(Call<RequestResult<User>> call, Throwable t) {}
                });
    }
    
    /*
    * ToDo:用户的cookie截取,
    * */
    public static String getSessionCookie(String cookieString) {
        if (!TextUtils.isEmpty(cookieString)) {
            String[] splitCookie = cookieString.split(";");
            String[] splitSessionId = splitCookie[0].split("=");
            cookieString = splitSessionId[1];
            return cookieString;
        }
        return "";
    }
    
    
    截取前__auth=2D034668A28C7B6E340B8C0333BCA20AF35B010B68E02313994182BA415AF4377A54425F56B8F2F45CC4DB41BFD42E958BC67F20A7CEAC60713D49BD56599EAF790E927F7C95F37BC2891ACD26795B3308AD1886B75E249F663011CE4C86743AE4640E1F19404D73BB249DED153867392D75EE6210D96AAD73FEEBDE9066047B27F9FAB62D84032B50E17751130DE1CD9BFBF5F8C3AB93575C470BCA48E4540C2BD77B0F7D4924FD7C279C21101E1E98CFFCDEB16EE9D673F1B5D0F6A16BBFA2C8F63A545631B1CC413B918069E408F3496CCB2E838599F2BC3B6E597AF0943D3ECD7288114E470B58FF130C340AAB02942D9EB91B5D004DE3DDD7384B3D76E175220A8EB59A5656B9160AC21F424793F493ACB97D619D37F28C31DE0C31183CFA9EFA3C55D631BBB9FCB8B057872123508AD6CE597BF79CB28CE79D610FA810F72E58F192093132F47CA8339E07B317D72AD840; expires=Fri, 14-Apr-2017 02:06:36 GMT; path=/; HttpOnly
    
    截取后2D034668A28C7B6E340B8C0333BCA20AF35B010B68E02313994182BA415AF4377A54425F56B8F2F45CC4DB41BFD42E958BC67F20A7CEAC60713D49BD56599EAF790E927F7C95F37BC2891ACD26795B3308AD1886B75E249F663011CE4C86743AE4640E1F19404D73BB249DED153867392D75EE6210D96AAD73FEEBDE9066047B27F9FAB62D84032B50E17751130DE1CD9BFBF5F8C3AB93575C470BCA48E4540C2BD77B0F7D4924FD7C279C21101E1E98CFFCDEB16EE9D673F1B5D0F6A16BBFA2C8F63A545631B1CC413B918069E408F3496CCB2E838599F2BC3B6E597AF0943D3ECD7288114E470B58FF130C340AAB02942D9EB91B5D004DE3DDD7384B3D76E175220A8EB59A5656B9160AC21F424793F493ACB97D619D37F28C31DE0C31183CFA9EFA3C55D631BBB9FCB8B057872123508AD6CE597BF79CB28CE79D610FA810F72E58F192093132F47CA8339E07B317D72AD840

    相关文章

      网友评论

          本文标题:Retrofit 2.0.1 获取请求头部

          本文链接:https://www.haomeiwen.com/subject/unyglttx.html