메뉴 건너뛰기

조회 수 4381 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
웹뷰를 이용한 간단한 어플을 하나 만들었는데.. 세션이 안되서 삽질을 많이 했습니다.
구글링을 통해 소스는 많이 있지만 자바코드에서 특정 웹페이지를 호출해서 session.getId() 를 찍어본 결과
호출 할 때 마다 다른 값이 나오더군요..뭐 근데.. 방법은 의외로 간단한 거였더군요.. 어쩌면 당연한 거였다는..

일단은 session.getId() 값이 같은 값이 나와야 합니다.. 이 값이 계속 바뀐다면 아무리 삽질을 한들 안되겠지요..
HttpClient httpclient =  new DefaultHttpClient();  이부분을 멤버변수로 바로 선언해 버립니다...
보통 다른 소스들은 호출할 때 마다 계속 재 생성하는데.. 재생성을 안하니 유지가 됩니다..ㅋ 그리고 JSESSIONID를 쿠키로 구우면 OK...

setSyncCookie() 안에 들어 있는 코드는 구글링을 통해 얻은 소스입니다.. 많이들 보셨을듯..하네요.. 바뀐부분은   List<Cookie> cookies = ((DefaultHttpClient)httpclient).getCookieStore().getCookies();
이부분인데.. HttpClient 는 getCookieStroe 메소드가 없어서 위처럼 형변환 해줬습니다..

쿠키로 구을 때 Log 찍어보시면 token값과 JSESSIONID도 같이 구워집니다..
그리고 CookieSyncManager.getInstance().startSync(); 가 되면
웹뷰와 연동 됩니다..  이렇게 안하면 웹뷰에서 접속하는 것과 HttpClient에서 접속하는 것이 따로 놀더군요...

응용해 보시길 바랍니다.. 더 좋은 방법이 있다면 조언해주시구요^^


public class AndroidTet extends Activity {
public static WebView webview;
public HttpClient httpclient =  new DefaultHttpClient();  //멤버변수로 선언
public CookieManager cookieManager;
public String domain = "http://192.168.0.44";
public void onCreate(Bundle savedInstanceState) {
   ...
        CookieSyncManager.createInstance(this);
        cookieManager = CookieManager.getInstance();
        CookieSyncManager.getInstance().startSync();
   ...
        setSyncCookie();
   ...
public void onResume() {
        super.onResume();
        CookieSyncManager.getInstance().startSync();
}

public void onPause() {
        super.onPause();
       
        if (CookieSyncManager.getInstance() != null) {
        CookieSyncManager.getInstance().stopSync();
        }
}
protected void onDestroy() {
        super.onDestroy();
       
        if (cookieManager != null) {
        cookieManager.removeAllCookie();
        }
}
public void setSyncCookie() {
        Log.e("surosuro", "token transfer start ---------------------------");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("token", "TEST");// 넘길 파라메터 값셋팅token=TEST
               
            HttpParams params = new BasicHttpParams();
           
            HttpPost post = new HttpPost(domain+/androidToken.jsp");
            post.setParams(params);
            HttpResponse response = null;
            BasicResponseHandler myHandler = new BasicResponseHandler();
            String endResult = null;

            try {
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            try {
                response = httpclient.execute(post);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                endResult = myHandler.handleResponse(response);
            } catch (HttpResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            List<Cookie> cookies = ((DefaultHttpClient)httpclient).getCookieStore().getCookies();
           
            if (!cookies.isEmpty()) {
                for (int i = 0; i < cookies.size(); i++) {
                    // cookie = cookies.get(i);
                    String cookieString = cookies.get(i).getName() + "="
                            + cookies.get(i).getValue();
                    Log.e("surosuro", cookieString);
                    cookieManager.setCookie(domain, cookieString);
                }
            }
            Thread.sleep(500);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
 

  1. 안드로이드 WebView 에서 tel: 이 되지않는 경우.

    Date2018.10.02 Views1633
    Read More
  2. Android Studio에서 SQLCipher 라이브러리 추가 방법

    Date2018.10.02 Views1776
    Read More
  3. 위젯 업데이트 주기 빠르게 하기

    Date2018.10.02 Views2142
    Read More
  4. MediaPlayer 클래스 사용법

    Date2018.10.02 Views1803
    Read More
  5. 하이브리드 앱에서의 세션관리(로그인 상태 유지)

    Date2018.12.27 Views5017
    Read More
  6. WebView를 사용할때 HttpClient를 이용한 Session 유지

    Date2018.12.27 Views4381
    Read More
  7. WebView 작업할때 Net::ERR_UNKNOWN_URL_SCHEME 에러 발생할때 (전화걸기,문자보내기 안된다)

    Date2020.12.14 Views1069
    Read More
  8. WebView에서 카메라 및 이미지 업로드 (선택적용가능)

    Date2020.12.14 Views2702
    Read More
  9. android.support.v4.content.FileProvider not found

    Date2020.12.14 Views308
    Read More
  10. Volley 로 웹요청하고 응답받기3 - Get방식 , json 읽기 (php,mysql)

    Date2020.12.14 Views378
    Read More
  11. 구글맵 snippet을 두줄이상으로 구현하기

    Date2020.12.14 Views481
    Read More
  12. 줄바꿈 문자 치환

    Date2020.12.14 Views298
    Read More
  13. Volley 이용시에 한글 깨질때 UTF-8로 변경

    Date2020.12.14 Views348
    Read More
  14. Fragment에서 Toast 사용하기

    Date2020.12.14 Views376
    Read More
  15. Fragment를 통한 하단탭 예제1

    Date2020.12.14 Views242
    Read More
  16. Volley 로 웹요청하고 응답받기2 - Post방식 , 로그인-회원가입 (php,mysql 연동)

    Date2020.12.14 Views1435
    Read More
  17. Volley 로 웹요청하고 응답받기1 - Get방식

    Date2020.12.14 Views348
    Read More
  18. 구글맵으로 GPS 현재위치 실시간 연동하기

    Date2020.12.14 Views2458
    Read More
  19. 안드로이드에서 url 주소로 이미지 바로 불러오기 (Glide 사용)

    Date2020.12.14 Views759
    Read More
  20. This Handler class should be static or leaks might occur 시 해결법

    Date2020.12.14 Views240
    Read More
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved