2016-10-18 2 views
1

짧은 이야기가 짧습니다. 내 앱이 시작될 때 인터넷에서 일부 Google 시트 파일을 다운로드하고 싶습니다. 상황은 다음과 같습니다.HTTP/1.1 302 일시적으로 이동 됨 - Android API에서 발생합니다. 16-17

최신 장치 : 모든 것이 100 % 작동하며 문제가 없으면 모든 파일이 다운로드됩니다.

이전 장치 (API 16-17) : 첫 번째 시트 파일을 올바르게 다운로드하십시오. 두 번째 파일을 다운로드하지 못했습니다.HTTP/1.1 302 일시적으로 이동 문제 및 파일 자체가 제대로 다운로드되지 않습니다.

두 파일 모두에 대해 AsynchTask를 호출합니다. AsynchTask가 완벽하게 실행되고 AsynchTask가 문제인지 확인하기 위해 다른 방법을 시도했습니다. 또한 모든 링크가 완벽하게 작동하고 작동합니다. 파일 다운로드

방법 :

private String downloadUrl(String urlString) throws IOException { 
    InputStream is = null; 

    try { 
     URL url = new URL(urlString); 
     HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 
     conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully. 
     HttpsURLConnection.setFollowRedirects(true); 
     conn.setReadTimeout(10000 /* milliseconds */); 
     conn.setConnectTimeout(15000 /* milliseconds */); 
     conn.setInstanceFollowRedirects(true); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     // Starts the query 
     conn.connect(); 
     int responseCode = conn.getResponseCode(); 
     is = conn.getInputStream(); 

     String contentAsString = convertStreamToString(is); 
     Log.d("contentAsString",contentAsString); 
     return contentAsString; 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 
} 

문자열 결과를 (HTTP/1.1 302 일시적으로 메시지 이전)

HTTP/1.1 302 Moved Temporarily 
     Content-Type: text/html; charset=UTF-8 
     Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
     Pragma: no-cache 
     Expires: Mon, 01 Jan 1990 00:00:00 GMT 
     Date: Tue, 18 Oct 2016 09:26:14 GMT 
     Location: https://docs.google.com/spreadsheets/d/1hRiDvdLPkQEdTSVxmWEoWXjmCFQodNjMNYi3Fd7yYn0/gviz/tq 
     P3P: CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info." 
     P3P: CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info." 
     Content-Encoding: gzip 
     X-Content-Type-Options: nosniff 
     X-XSS-Protection: 1; mode=block 
     Server: GSE 
     Set-Cookie: NID=89=afVN4Sa74ZsYuffxNSiXn2wWTJkSUULbtZperbpr8T9hPzHoFzx-uGu_lJUVCkYSd1eZUPUFucffCDHc7lPConnfPpTMbqAOgIcIQoJG6TQFUHzBUNW6bFqUy__ZthsR;Domain=.google.com;Path=/;Expires=Wed, 19-Apr-2017 09:26:14 GMT;HttpOnly 
     Set-Cookie: NID=89=k5r33ZLA4l__4v1CE1iGrtQbtqoJxOyVxwrSMbsKWviK74u-vM32WdKtt-txFEOhPWo1g9f1CWMXcu6Fuczo4ZCck47D23tIZZRcqpRxkSB0z5w2xI9oj1Jcq8duISiU;Domain=.google.com;Path=/;Expires=Wed, 19-Apr-2017 09:26:14 GMT;HttpOnly 
     Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32" 
     Transfer-Encoding: chunked 

     00000001 
     
     00000001 
     � 
     00000001 
     
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     �� 
     00000001 
     m 
     00dc 
     ��N�0D�� 
     �ܳ�8 !7R�8)ih�hs��U�R�!6��q 
     G����HoY!�2�X����,J�TfFI��L�4��~����Z�~��eݬ�]D����E��.�k�n$i��/���r,%E��U΍��gl�ӟ1> 
     v���V!:���Y�/�;.��ۗ�s�?��&��=U�v���õ�,���op4Q8!�4�=� 
     %��O����ڛ����� 
     0 

후 다음

내 AsynchTask 클래스 코드의 일부이다 HTTP 302에 대해 조금 읽는 것. 어떻게 든 리디렉션해야한다는 것을 알고 있습니다. 문제는 어떻게해야할지 모르겠다. 또한, 나는 왜이 문제는 나이가 안드로이드 버전에서만 나타납니다 모르겠어요.

저는 일주일 동안 고생했습니다. 모든 의견을 높이 평가! 고맙습니다! 리디렉션을 다음의 핵심은 키 location에 의해 헤더를 가지고 대신 URL로 것을 사용하는 것입니다

답변

2

:

Location: https://docs.google.com/spreadsheets/d/1hRiDvdLPkQEdTSVxmWEoWXjmCFQodNjMNYi3Fd7yYn0/gviz/tq 

그것은 말하는 바로 웹 서버의 방법은 "이봐, 당신의 페이지는 여기 이동 거기로 가라. "

package com.mkyong.http; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class HttpRedirectExample { 
    public static void main(String[] args) { 
     try { 
      String url = "http://www.twitter.com"; 

      URL obj = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); 
      conn.setReadTimeout(5000); 
      conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
      conn.addRequestProperty("User-Agent", "Mozilla"); 
      conn.addRequestProperty("Referer", "google.com"); 

      System.out.println("Request URL ... " + url); 

      boolean redirect = false; 

      // normally, 3xx is redirect 
      int status = conn.getResponseCode(); 
      if (status != HttpURLConnection.HTTP_OK) { 
       if (status == HttpURLConnection.HTTP_MOVED_TEMP 
       || status == HttpURLConnection.HTTP_MOVED_PERM 
       || status == HttpURLConnection.HTTP_SEE_OTHER) 
        redirect = true; 
      } 

      System.out.println("Response Code ... " + status); 

      if (redirect) { 
       // get redirect url from "location" header field 
       String newUrl = conn.getHeaderField("Location"); 

       // get the cookie if need, for login 
       String cookies = conn.getHeaderField("Set-Cookie"); 

       // open the new connnection again 
       conn = (HttpURLConnection) new URL(newUrl).openConnection(); 
       conn.setRequestProperty("Cookie", cookies); 
       conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
       conn.addRequestProperty("User-Agent", "Mozilla"); 
       conn.addRequestProperty("Referer", "google.com"); 

       System.out.println("Redirect to URL : " + newUrl); 
      } 

      BufferedReader in = new BufferedReader(
      new InputStreamReader(conn.getInputStream())); 
      String inputLine; 
      StringBuffer html = new StringBuffer(); 

      while ((inputLine = in.readLine()) != null) { 
       html.append(inputLine); 
      } 
      in.close(); 

      System.out.println("URL Content... \n" + html.toString()); 
      System.out.println("Done"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

나는 그것을 확인해 보겠습니다 :

article by mkyong.com 좋은 예제가! 고맙습니다. 피드백과 함께 돌아올 것입니다! – AresProductions

+0

지금 일하는 것 같습니다! 고마워요 :) – AresProductions

+0

: thumbsup : 좋은 소식 :) – Knossos