메뉴 건너뛰기

조회 수 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 Activity Data Transfor/ 액티비티 이동간에 데이터 전송하기 file 2016.06.07 7676
256 Activity Switching / 안드로이드 액티비티 전환 / 화면 전환 file 2016.06.07 8311
255 Android Login and Registration with PHP, MySQL and SQLite file 2015.07.16 14178
254 Android Navigation Drawer API 공개! 디자인 가이드 살펴보기 file 2015.07.29 8141
253 Android Push GCM 서버 구성 하기(3) file 2015.12.14 6388
252 Android Push GCM 프로젝트 앱 적용 하기(2) file 2016.03.18 8953
251 android SMS 리시버 2015.06.29 6871
250 Android Studio에서 SQLCipher 라이브러리 추가 방법 file 2018.10.02 1776
249 Android TIP] strings.xml 에서 특수문자 사용하기 2015.12.15 6629
248 Android 간단한 회원 가입 폼 만들기 for Mac (PHPMyAdmin 이용) file 2015.07.10 10508
247 Android 와 JSP 간 파라미터 암복호화 (1) file 2016.05.26 7474
246 Android 와 JSP 간 파라미터 암복호화 (2) 2016.05.26 7735
245 Android 와 JSP 간 파라미터 암복호화 (3) file 2016.05.26 8088
244 android.support.v4.content.FileProvider not found file 2020.12.14 308
243 AndroidManifest에 선언한 메타데이터(meta-data) 가져오기 2016.06.10 9313
242 Android] Fragment 내부의adapter에서 startActivity 하기 2015.12.15 6487
241 Android] 안드로이드 홈 디렉토리 알아내기 2015.12.15 6895
240 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1619
239 App 실행 file 2021.03.31 244
238 CSS3 Rounded Corner, 그림자 효과 사용하기 file 2014.09.04 6760
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved