2012-05-14 8 views
1

私はちょうどデータを解析するためのページのソースを取得したいと思います。
私は
アンドロイドのページ全体を取得できません

public static String getPageSourceFromUrl(String Url) { 
     String text = ""; 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(Url); 
      HttpResponse response = client.execute(request); 
     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(); 
     text = str.toString(); 
    } catch (Exception ex) { 

    } 
    return text; 
} 

次のコードを使用してページがある:「http://vn.answers.yahoo.com/question/index?qid=20111022210730AAWvfKI」は

残念ながら、(上記のような)一部のページでは、テキストのリターンは、ソース全体のほんの一部であり、 。文字列が制限を超えている可能性がありますので、誰でも解決策がありますか?

+0

あなたはこれを解決するために管理していましたか? – erdomester

+0

申し訳ありません、それは非常に長い時間前ですので、私はここで尋ねるものを覚えていません:( –

+0

私はこの問題を解決することができた私のソリューションを受け入れてください。 – erdomester

答えて

0

私は(私たちの)問題を解決することができました。エレガントな方法は、クラスを作成することです:

public class GetRating { 

    public GetRating(){ 

    } 

    public String GetRatingFromURL(String url){ 

     HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client 
     HttpGet httpget = new HttpGet(url); // Set the action you want to do 
     HttpResponse response = null; 
     try { 
      response = httpclient.execute(httpget); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } // Executeit 


     HttpEntity entity = response.getEntity(); 
     InputStream is = null; 


     try { 
      is = entity.getContent(); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } // Create an InputStream with the response 


     BufferedReader reader = null; 
     try { 
      reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


     StringBuilder sb = new StringBuilder(); 
     String line = null; 


     try { 
      while ((line = reader.readLine()) != null) // Read line by line 
       sb.append(line + "\n"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     String resString = sb.toString(); // Result is here 


     return resString; 


    } 


} 

そして、別のクラスからそれを呼び出す:

GetRating GN = new GetRating(); 
String pagesource = GN.GetRatingFromURL(url)("http://somewebpage.com"); 
関連する問題