메뉴 건너뛰기

조회 수 4380 추천 수 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();
        }
    }
}
 

List of Articles
번호 제목 날짜 조회 수
257 Effects - Animate() 메서드 (여러가지 효과 동시 처리) file 2014.10.16 30624
256 안드로이드와 mysql 연동시키기. php 와 json 사용 file 2015.07.16 24488
255 [DB] 서버/클라이언트 소켓 통신하기 2015.07.13 20567
254 월별 캘린더에 일정 입력 및 조회 기능 리스트로 추가하기 file 2015.07.16 18552
253 스토리보드 짜는 방법 file 2015.07.16 15419
252 카카오톡 대화내용 가져오기(sqlite3, chat_logs) file 2016.05.26 15117
251 간단한 mp3 플레이어 만들기 , 음악넣기 , 노래재생 file 2016.06.07 14623
250 TextureView를 이용한 카메라 Preview 좌우 반전 2015.06.10 14212
249 Android Login and Registration with PHP, MySQL and SQLite file 2015.07.16 14178
248 블루투스(Bluetooth) 통신에 대해 알아보자 file 2015.07.26 14047
247 사진찍기 및 앨범 에서 사진 가져오기!!! 2014.08.28 13889
246 EditText의 글자 수 제한 걸기 2015.07.16 13881
245 [DB]Android - DB 연동 기술 정리 2015.07.13 13798
244 노티피케이션(Notification) 사용법 / Notification.Builder , NotificationManager file 2016.06.10 13470
243 안드로이드] 페이스북 같은 슬라이드 메뉴 만들기 file 2015.12.15 12536
242 안드로이드용 채팅프로그램 클라이언트(java), 서버(c#) 소스 file 2016.05.19 11722
241 안드로이드에서 JSP 를 사용하여 mysql 연동하기 2015.07.16 11685
240 WIFI 신호세기 강도 측정하기 2014.08.28 11243
239 블루투스 및 비콘 관련 정리 2015.07.26 10828
238 Android 간단한 회원 가입 폼 만들기 for Mac (PHPMyAdmin 이용) file 2015.07.10 10508
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved