2014-07-22 2 views
0

모든 관련 게시물을 찾을 수 있지만 여전히 내 문제를 해결할 수 없습니다. 내 응용 프로그램은 org.apache.http.cookie.Cookie 개체로 저장된 세션 쿠키를 사용하여 서버와 통신합니다. 내 연결에 HttpClient을 사용하면 정상적으로 작동합니다.HttpUrlConnection에 대한 쿠키를 설정하는 방법

인증 :

 List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
     if (!cookies.isEmpty()) { 
      sessionCookie = cookies.get(0); 
      /** multiple cookies usage can be implemented if needed */ 
     } 

서버에 각 POST : 그것은 쿠키에 관해서하지만 난 적어도 내 의견합니다 (Cookie 객체의 모양을 알 수있을 때 내가 좀 새로운 해요

 CookieStore store = client.getCookieStore(); 
     HttpContext ctx = new BasicHttpContext(); 
     store.addCookie(Tools.getSessionCookie()); 
     ctx.setAttribute(ClientContext.COOKIE_STORE, store); 

)는 여러 개의 키 - 값을 가지고 JSONObject과 조금 비슷합니다. 이제 LazyList을 사용하여 많은 이미지를 GridView에로드하려고했습니다. 내가 알아 낸 ImageLoader 클래스를 보면 그것은 HttpUrlConnection 사용

private Bitmap getBitmap(String url) { 
    File f = fileCache.getFile(url); 
    // from SD cache 
    Bitmap b = decodeFile(f); 
    if (b != null) 
     return b; 

    // from web 
    try { 
     Bitmap bitmap = null; 
     URL imageUrl = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) imageUrl 
       .openConnection(); 
     //timeouts modified 
     conn.setConnectTimeout(GetSettings.getTimeout(context, 
       AppConstants.FLAG_CONN_TIMEOUT)); 
     conn.setReadTimeout(GetSettings.getTimeout(context, 
       AppConstants.FLAG_SO_TIMEOUT)); 
     conn.setInstanceFollowRedirects(true); 
     InputStream is = conn.getInputStream(); 
     OutputStream os = new FileOutputStream(f); 
     Utils.CopyStream(is, os); 
     os.close(); 
     conn.disconnect(); 
     bitmap = decodeFile(f); 
     return bitmap; 
    } catch (Throwable ex) { 
     ex.printStackTrace(); 
     if (ex instanceof OutOfMemoryError) 
      memoryCache.clear(); 
     return null; 
    } 
} 

내가 세션 쿠키를 설정하고 물론 내가 서버 응답으로 401 unauthorized을 얻고 수정할 수 없습니다. 기본적으로 제가 가지고있는 것은 org.apache.http.cookie.Cookie 객체입니다. 시도했으나 작동하지 않았습니다.

내 경우에 세션 쿠키를 사용하는 올바른 방법은 무엇입니까?

답변

0

이제 HttpClient을 사용하는 방식으로 ImageLoader 클래스를 수정했습니다. 그러나 나는 아직도 HttpUrlConnection을 사용하여 세션 쿠키를 설정하는 방법을 찾고 있으므로 허용 된대로 내 자신의 답변을 표시하지는 않습니다.