Volley 추가 쿠키 요약

최근에 Volley 프레임워크를 교체하는 작업을 했습니다. 먼저 Volley가 매우 강하고 네트워크 대기열에 대한 요청, 이미지 다운로드 캐시 메커니즘이 모두 최적화되었습니다.현재 매우 자주 사용되는 중요한 네트워크 요청 프레임워크다.그러나 Volley는 쿠키 추가는 지원하지 않지만addHeader 방법으로 추가할 수도 있다.
우선StringRequest, JosonRequest, ImageRequest는 모두 Volley를 계승하는 핵심 클래스이다
getHeaders () 방법을 다시 쓰면 됩니다.
private Map sendHeader = new HashMap(1);
@Override
    public Map getHeaders() throws AuthFailureError {
//            ,          
        String cookies = "";
        cookies = "visit-source=" + VERSION_SOURCE + ";" + "channel=" + Utils.getChannelValue
                (MainApplication.getApplication()) + ";" + "current-version=" + versionName + ";"
                + PreferenceFile.getResponseCookies(MainApplication.getApplication()
                .getApplicationContext());
        if (!TextUtils.isEmpty(token)) {
 + token);
            cookies = cookies + ";" + "adw_id=" + token;
        }
        sendHeader.put("cookie",cookies);
        return sendHeader;
    }

또한, 서버가 전송하는 쿠키가 필요할 때가 있는데 요청할 때 다시 보내면 어떡하죠, 우선 백스테이지에서 전송되는 쿠키가
Request에서 아주 중요한 방법이 하나 있는데parseNetworkResponse입니다. 방법을 바꾸면
@Override
    protected Response parseNetworkResponse(NetworkResponse response) {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            
            LogUtils.logD("Volley POST Response: ", "get data in parseNetworkResponse " + new String(response.data));
            
            String responseCookiesStr = response.headers.get("Set-Cookie");
            //                  ,       request            
            if (responseCookiesStr!= null && responseCookiesStr.contains("BUID")) {
                PreferenceFile.saveResponseCookies(MainApplication.getApplication().getApplicationContext(), responseCookiesStr);
            }

            //  jsonObject  deliverResponse  ,        onResponse   
            JSONObject jsonObject = new JSONObject(jsonString);
            LogUtils.logD("Volley JsonObjectPOST: ", "jsonObject " + jsonObject.toString());

            return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
       
    }

그리고 최근에 이 부분을 처리할 때 Volley 프레임워크를 사용하지 않을 때 백스테이지와 상호작용을 하지 않았습니다. 나중에 너무 Nginx가 자동으로 쿠키를 추가했지만 Volley를 교체한 후에 Volley가 스위치를 꺼서 그런지 어떻게 해야 할지 모르겠습니다. 밑에 있는 코드를 봐도 찾을 수 없습니다.그래서 헤더 파일을 추가할 때 백엔드에 필요한 쿠키, 즉 위의 양쪽 코드를 추가해서 해결한다.

좋은 웹페이지 즐겨찾기