2012-06-07 3 views
2

현재 미디어 플레이어가 있으며 원본 경로에서 리디렉션 주소를 가져 오려고합니다. 미디어 플레이어가 리디렉션 처리를 지원하지 않기 때문에 httpurlconnection 등을 만들어 리다이렉트 된 URL 경로를 얻으려고합니다. 그러나 제대로 수행하고 있는지 확실하지 않습니다. 어떤 도움을 주시면 감사하겠습니다. 감사.Android - URL 리디렉션 받기

코드 :

Log.d(TAG, "create url - test"); 
URL testUrl = new URL(path); 
HttpURLConnection conn = (HttpURLConnection)testUrl.openConnection(); 

String test = conn.getURL().toString(); 
String test1 = conn.getHeaderField(2); 
String test2 = conn.toString(); 
Log.d(TAG, "normal stuff test is: " + test); 
Log.d(TAG, "header field test is: " + test1); 
Log.d(TAG, "url to string is: " + test2); 
+0

내 대답 [여기] (http://stackoverflow.com/questions/10341475/getting-url-after-a-redirect-using-httpclient-executehttpget)이 도움이되는지 확인하십시오. – yorkw

답변

0

코드는 아래 URL 중 하나 홉 리디렉션 다음과 같습니다. GET 대신 HTTP HEAD 요청을 사용하면을 사용하여 은 근본적으로 적은 대역폭을 소모합니다. 여러 홉을 처리하기 위해이 메서드를 확장하는 것이 상당히 간단합니다.

public URI followRedirects(URI original) throws ClientProtocolException, IOException, URISyntaxException 
{ 
    HttpHead headRequest = new HttpHead(original); 

    HttpResponse response = client.execute(headRequest); 
    final int statusCode = response.getStatusLine().getStatusCode(); 
    if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || 
     statusCode == HttpStatus.SC_MOVED_TEMPORARILY) 
    { 
     String location = response.getHeaders("Location")[0].toString(); 
     String redirecturl = location.replace("Location: ", ""); 
     return new URI(redirecturl); 
    } 
    return original; 
} 

그것은 이미 필드 client에 저장된 HttpClient을 설정 한 가정합니다.

this question도 참조하십시오.