0

HttpURLConnection을 사용하여 XML 파일을 다운로드하려고하는이 재미있는 문제가 있습니다. 코드는 안드로이드 4.2에서 잘 작동하지만 어떤 도움이HttpURLConnection은 Android Gingerbread를 사용할 수 없지만 JellyBean에서 제대로 작동합니다.

이해할 수있는 코드는 아래와 같습니다 2.3.4

작동하지 않습니다.

package com.test.testdownloader; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ((Button) findViewById(R.id.button1)) 
       .setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View arg0) { 
         try { 
          downloadtest(); 
         } catch (IOException e) { 
          e.printStackTrace(); 
         } 

        } 
       }); 
    } 

    void downloadtest() throws IOException { 

     URL url = new URL("http://someurl/list.xml"); 
     new DownloadTask().execute(url); 

    } 

    private class DownloadTask extends AsyncTask<URL, Integer, String> { 

     @Override 
     protected String doInBackground(URL... urls) { 
      try { 
       URL url = urls[0]; 
       InputStream is = inputStreamForUrl(url); 
       String result = getStringFromInputStream(is); 
       System.out.println(result); 
       return result; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

    } 

    // convert InputStream to String 
    private static String getStringFromInputStream(InputStream is) { 

     BufferedReader br = null; 
     StringBuilder sb = new StringBuilder(); 

     String line; 
     try { 

      br = new BufferedReader(new InputStreamReader(is)); 
      while ((line = br.readLine()) != null) { 
       sb.append(line); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (br != null) { 
       try { 
        br.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     return sb.toString(); 

    } 

    public static InputStream inputStreamForUrl(URL url) throws IOException { 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoInput(true); 
     urlConnection.setConnectTimeout(30000); 
     urlConnection.setReadTimeout(30000); 
     urlConnection.connect(); 
     return urlConnection.getInputStream(); 
    } 

} 

그것은 안드로이드 4.2과 4.3에서 잘 작동하지만하지 여기에, 발리를 사용한다 2.3.4

08-21 11:15:55.634: W/System.err(5719): java.net.SocketTimeoutException: Connection timed out 
08-21 11:15:55.644: W/System.err(5719):  at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method) 
08-21 11:15:55.644: W/System.err(5719):  at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357) 
08-21 11:15:55.654: W/System.err(5719):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204) 
08-21 11:15:55.654: W/System.err(5719):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437) 
08-21 11:15:55.654: W/System.err(5719):  at java.net.Socket.connect(Socket.java:983) 
08-21 11:15:55.664: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75) 
08-21 11:15:55.664: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
08-21 11:15:55.664: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 
08-21 11:15:55.674: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
08-21 11:15:55.674: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
08-21 11:15:55.674: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
08-21 11:15:55.684: W/System.err(5719):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
08-21 11:15:55.684: W/System.err(5719):  at com.test.testdownloader.MainActivity.inputStreamForUrl(MainActivity.java:99) 
08-21 11:15:55.694: W/System.err(5719):  at com.test.testdownloader.MainActivity$DownloadTask.doInBackground(MainActivity.java:51) 
08-21 11:15:55.694: W/System.err(5719):  at com.test.testdownloader.MainActivity$DownloadTask.doInBackground(MainActivity.java:1) 
08-21 11:15:55.694: W/System.err(5719):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
08-21 11:15:55.694: W/System.err(5719):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
08-21 11:15:55.694: W/System.err(5719):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
08-21 11:15:55.694: W/System.err(5719):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
08-21 11:15:55.694: W/System.err(5719):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
08-21 11:15:55.694: W/System.err(5719):  at java.lang.Thread.run(Thread.java:1019) 
+0

에뮬레이터 또는 다른 장치에서 테스트 중이십니까? – Vamshi

+0

장치는 에뮬레이터에서 절대로 시도하지 않았습니다. – Tirtha

답변

2

그것은 프록시 설정에 문제가 있었다. 안드로이드 4.2는 프록시를 사용하고 있었지만 2.3은 프록시를 구성하지 않았습니다.

그러나 무선> 네트워크 및 무선> Wi-Fi 설정> [메뉴 버튼]> 고급 설정에서 프록시를 구성하면 응용 프로그램이 해당 위치에서 프록시를 선택하지 않습니다.

내가 코드에서 프록시를 언급하는 경우

,

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.url", 8080)); 
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(proxy); 

는 그것을 잘 작동합니다.

0

나의 제안이 오류가 발생합니다 아주 작은 샘플입니다 :

발보리에 JarJar으로

그러나 나는 당신에게 백분율을주고, 도움이 될 수 있습니다

public static class Connector extends AsyncTask<String, Object, Object> { 
    private static final int DEFAULT_CNN_TIME_OUT = 1000 * 20; 
    private static String USER_AGENT; 
    private String   mUrl; 
    private byte[]   mBody; 

    public Connector(Context _cxt) { 
     super(); 
     if(TextUtils.isEmpty(USER_AGENT)) { 
      // Without this, the default provided by API will be like "Dalvik/1.6.0 (Linux; U; Android 4.0.4; BASE_Lutea_3 Build/IMM76D)" . 
      USER_AGENT = new WebView(_cxt).getSettings().getUserAgentString(); 
     } 
    } 

    /* 
    * Convenient function to execute the connecting task. 
    */ 
    public void submit(String _url) { 
     this.execute(_url); 
    } 

    @Override 
    protected Object doInBackground(String... _params) { 
     mUrl = _params[0]; 
     Object ret = null; 
     HttpURLConnection conn = null; 
     try { 
      synchronized(Connector.class) { 
       InputStream in = null; 
       conn = connect(mUrl); 
       // if we don't do conn.setRequestProperty("Accept-Encoding", "gzip"), the wrapper GZIPInputStream can be removed. 
       onConnectorInputStream(in = new GZIPInputStream(conn.getInputStream())); 
       in.close(); 
       in = null; 
      } 
     } 
     catch(Exception _e) { 
      ret = _e; 
     } 
     finally { 
      if(conn != null) { 
       conn.disconnect(); 
       conn = null; 
      } 
     } 
     return ret; 
    } 

    @Override 
    protected void onPostExecute(Object _result) { 
     if(_result instanceof SocketTimeoutException) { 
      onConnectorConnectTimout(); 
     } else if(_result instanceof ConnectorPostConnectException) { 
      onConnectorError(((ConnectorPostConnectException) _result).getStatus()); 
     } else if(_result instanceof Exception) { 
      onConnectorInvalidConnect((Exception) _result); 
     } else if(_result == null) { 
      onConnectorFinished(); 
     } 
     handleEstablishedConnection(); 
    } 

    /* 
    * Internal help and test function. 
    */ 
    private static void handleEstablishedConnection() { 
     try { 
      Log.v(TAG, "Connection is established."); 
      CookieStore cs = CookieManager.getInstance().getCookieStore(); 
      if(cs != null) { 
       Log.v(TAG, "------------cookies------------"); 
       List<Cookie> list = cs.getCookies(); 
       if(list != null && list.size() > 0) { 
        StringBuilder cookieBuilder = new StringBuilder(); 
        for(Cookie c : list) { 
         cookieBuilder 
           .append(c.getName().trim()) 
           .append("=>") 
           .append(c.getValue().trim()) 
           .append("=>") 
           .append(c.getDomain()); 
         Log.v(TAG, cookieBuilder.toString()); 
         cookieBuilder.delete(0, cookieBuilder.length() - 1); 
        } 
        cookieBuilder = null; 
       } else { 
        Log.v(TAG, "Empty cookies."); 
       } 
       cs = null; 
       list = null; 
      } 
     } 
     catch(Exception _e) { 
      Log.e(TAG, "Error in handleEstablishedConnection: " + _e.getMessage()); 
     } 
     finally { 
     } 
    } 

    private HttpURLConnection connect(String _urlStr) throws Exception { 
     URL url = null; 
     HttpURLConnection conn = null; 
     try { 
      try { 
       url = new URL(_urlStr); 
      } 
      catch(MalformedURLException e) { 
       throw new IllegalArgumentException("Invalid url: " + _urlStr); 
      } 
      conn = preConnect(url); 
      doConnect(conn); 
      conn = postConnect(conn); 
     } 
     catch(Exception _e) { 
      throw _e; 
     } 
     finally { 
      url = null; 
     } 
     return conn; 
    } 

    private HttpURLConnection preConnect(URL url) throws Exception { 
     HttpURLConnection conn; 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setUseCaches(false); 
     // http://www.aswinanand.com/2009/01/httpurlconnectionsetfollowredirects-bug/comment-page-1/#comment-13330 
     // see the url to learn more about the problem of redirect 
     conn.setInstanceFollowRedirects(false); 
     conn.setDoOutput(true);// allows body 
     mBody = getBody(); 
     if(hasBody()) { 
      conn.setFixedLengthStreamingMode(mBody.length); 
     } 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
     conn.setRequestProperty("User-Agent", USER_AGENT); 
     conn.setRequestProperty("Accept-Encoding", "gzip");// force server to send in Content-Encoding: gzip . 
     String cookies = onCookie(); 
     if(!TextUtils.isEmpty(cookies)) { 
      conn.setRequestProperty("Cookie", onCookie()); 
      cookies = null; 
     } 
     conn.setConnectTimeout(onSetConnectTimeout()); 
     return conn; 
    } 

    /* 
    * Convenient function to check the exiting of body. 
    */ 
    private boolean hasBody() { 
     return mBody != null && mBody.length > 0; 
    } 

    private void doConnect(HttpURLConnection conn) throws Exception { 
     OutputStream out; // the outgoing stream 
     out = conn.getOutputStream(); 
     if(hasBody()) { 
      out.write(mBody); 
     } 
     out.close(); 
     out = null; 
    } 

    private HttpURLConnection postConnect(HttpURLConnection conn) throws Exception { 
     int status = conn.getResponseCode(); 
     if(status != HttpURLConnection.HTTP_OK) { 
      throw new ConnectorPostConnectException(status); 
     } else { 
      CookieManager.getInstance().put(conn.getURL().toURI(), conn.getHeaderFields()); 
      return conn; 
     } 
    } 

    private byte[] getBody() { 
     byte[] body = null; 
     String bodyString = onSetBody(); 
     if(!TextUtils.isEmpty(bodyString)) { 
      body = bodyString.getBytes(); 
     } 
     return body; 
    } 

    // ------------------------------------------------ 
    // Overrides methods here 
    // ------------------------------------------------ 

    protected int onSetConnectTimeout() { 
     return DEFAULT_CNN_TIME_OUT; 
    } 

    protected String onCookie() { 
     return null; 
    } 

    protected String onSetBody() { 
     return null; 
    } 

    protected void onConnectorConnectTimout() { 
     Log.e(TAG, "Handling connector timeout gracefully."); 
    } 

    protected void onConnectorError(int _status) { 
     Log.e(TAG, "Handling connector error(responsed) gracefully: " + _status); 
    } 

    protected void onConnectorInvalidConnect(Exception _e) { 
     Log.e(TAG, "Handling connector invalid connect(crash) gracefully: " + _e.toString()); 
    } 

    /* 
    * Read data here. The function runs in thread. To hook on UI thread use onConnectorFinished() 
    */ 
    protected void onConnectorInputStream(InputStream _in) { 
    } 

    /* 
    * Last handler for a success connection 
    */ 
    protected void onConnectorFinished() { 
    } 
} 
관련 문제