0

私は以下のコードを使用してURLからhtmlソースを取得することを意味します。しかし、すべてのソースが含まれているわけではありません。Buffersizeは問題ですか、それとも文字列サイズの問題ですか?

HttpURLConnection connection; 
            OutputStreamWriter request = null; 

                 URL url = null;    
                 String response = null;          
                 String parameters = "aranan="+et.getText();    

                 try 
                 { 
                     url = new URL("http://www.fragmanfan.com/arama.asp"); 
                     connection = (HttpURLConnection) url.openConnection(); 
                     connection.setDoOutput(true); 
                     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

                     request = new OutputStreamWriter(connection.getOutputStream()); 
                     request.write(parameters); 
                     request.flush();             
                     String line = "";                
                     InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
                     BufferedReader reader = new BufferedReader(isr); 
                     StringBuilder sb = new StringBuilder(); 
                     while ((line = reader.readLine()) != null) 
                     { 
                         sb.append(line + "\n"); 
                     } 
                     // Response from server after login process will be stored in response variable.                 
                     response = sb.toString(); 
                     // You can perform UI operations here 
                     browser.loadDataWithBaseURL(null, response,"text/html", "UTF-8", null); 

                     isr.close(); 
                     reader.close(); 

                 } 
                 catch(IOException e) 
                 { 
                     // Error 
                 } 




        } 
    }); 

私は次のようなことを試みます が、うまくいきBufferedReader reader = new BufferedReader(isr,8192); ません。

4

2 に答える 2

1

WebRequestクラスを作成します。あなたの要求をして、応答を得るより。私はそのウェブサイトを試しました、それは動作します。

WebRequest response = new WebRequest("http://www.fragmanfan.com/arama.asp?aranan=kurtlar", PostType.GET);
String htmltext = response.Get();
browser.loadDataWithBaseURL(null, htmltext, "text/html", "UTF-8", null);

WebRequest.class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class WebRequest {
  public enum PostType{
    GET, POST;
  }

  public String _url;
  public String response = "";
  public PostType _postType;
  CookieStore _cookieStore = new BasicCookieStore();

  public WebRequest(String url) {
    _url = url;
    _postType = PostType.POST;
  }

  public WebRequest(String url, CookieStore cookieStore) {
    _url = url;
    _cookieStore = cookieStore;
    _postType = PostType.POST;
  }

  public WebRequest(String url, PostType postType) {
    _url = url;
    _postType = postType;
  }

  public String Get() {
    HttpClient httpclient = new DefaultHttpClient();

    try {
      // Create local HTTP context
      HttpContext localContext = new BasicHttpContext();

      // Bind custom cookie store to the local context
      localContext.setAttribute(ClientContext.COOKIE_STORE, _cookieStore);

      HttpResponse httpresponse;
      if (_postType == PostType.POST)
      {
        HttpPost httppost = new HttpPost(_url);
        httpresponse = httpclient.execute(httppost, localContext);
      }
      else
      {
        HttpGet httpget = new HttpGet(_url);
        httpresponse = httpclient.execute(httpget, localContext);
      }

      StringBuilder responseString = inputStreamToString(httpresponse.getEntity().getContent());

      response = responseString.toString();
    }
    catch (UnknownHostException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      // When HttpClient instance is no longer needed,
      // shut down the connection manager to ensure
      // immediate deallocation of all system resources
      httpclient.getConnectionManager().shutdown();
    }

    return response;
  }

  private StringBuilder inputStreamToString(InputStream is) throws IOException {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is,Charset.forName("iso-8859-9")));
    // Read response until the end
    while ((line = rd.readLine()) != null) {
      total.append(line);
    }

    // Return full string
    return total;
  }
}
于 2012-04-12T18:52:54.390 に答える
0

問題がありました。

私は使用 Log.i("tag", html) しましたが、ロガーには最大長のメッセージがあります。そして私のhtmlテキストは切り取られました。2つの解決策があります:

  1. HTMLを細かく分割します
  2. この投稿のように、メッセージの最大長のサイズを大きくします: リンク
于 2013-04-02T11:14:53.253 に答える