2012-09-10 63 views
0

我有這樣的功能:這個get-HTML-from-Url-Function中的錯誤在哪裏?

public String getUrl(String url) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      String html = ""; 
      try { 
        CookieStore cookieStore = new BasicCookieStore(); 
        HttpContext localContext = new BasicHttpContext(); 
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

        HttpGet httpget = new HttpGet(url); 

        // Pass local context as a parameter 
        HttpResponse response = httpclient.execute(httpget, localContext); 

        InputStream in = response.getEntity().getContent(); 
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(in)); 
        StringBuilder str = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
          str.append(line); 
        } 
        in.close(); 
        html = str.toString(); 

      } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } finally { 
        httpclient.getConnectionManager().shutdown(); 
        return html; 
      } 
    } 

的問題是,這些功能不會返回任何字符(我和google.de嘗試了一下)。可悲的是,結果總是空的。

THX的幫助

+0

任何stacktrace可能? – njzk2

回答

0

我的猜測(我只能用您提供的信息猜測):

你忘了清單中的Internet權限。

+0

已關閉。這是您可以考慮的最簡單的問題:1)我對互聯網許可和第二次進行了格式化;我的手機失去了聯繫(我自己嘲笑自己)。感謝您的幫助 :-) – user1508609