Android 如何获取html的所有源代码?
我的意思是,我使用下面的代码从url获取html源代码。但它并不包含所有源代码。缓冲区大小是问题还是字符串大小问题Android 如何获取html的所有源代码?,android,string,inputstream,bufferedreader,Android,String,Inputstream,Bufferedreader,我的意思是,我使用下面的代码从url获取html源代码。但它并不包含所有源代码。缓冲区大小是问题还是字符串大小问题 HttpURLConnection connection; OutputStreamWriter request = null; URL url = null; String response = null; String
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=newbufferedreader(isr,8192)代码>
但是它不起作用。创建一个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;
}
}
我遇到了一些问题
我使用了Log.I(“tag”,html)
但是记录器有一个最大长度的消息。我的html文本被剪裁了。
有两种解决方案:
将html拆分为小块
使邮件的最大长度变大,如本文所示:
您是否尝试过使用相同url的普通独立java应用程序获取源代码?否。不是java。但是ı没有得到完整的html页面源代码。几乎只有892个单词。字符串大小问题?还是缓冲区?ı如何解决这个问题?首先尝试在java中运行相同的逻辑。这样你就可以很容易地了解发生了什么。如果这在java中起作用,那么它在android中也应该起作用。ı如何尝试。是相同的代码吗?它可以在android上运行,但并不包含所有html源代码:(我们使用double而不是int。所以有没有比字符串大的东西我使用的是string的isntead。但它仍然不包含所有源页面。请尝试WebRequest response=new WebRequest(“,PostType.GET);这是我在WebView上看到的页面页脚的屏幕截图。我还调试了从请求返回的页面的最后100个字符。你可以看到页面末尾的标记。你是否因为你的错误而尝试记录.d整个页面在日志控制台窗口上看不到那么大的文本数据,它会在一段时间后出现。但如果您将响应附加到webview,则可以使用它。log.d(“,”+htmltext.substring(htmltext.length()-100));d/(351):t>`:(pffff:(为什么我没有它。我在你的代码中看到你成功了,但当我尝试它时,只有一部分源代码…:(为什么为什么为什么/为什么你认为你得到了部分源代码?你能发布一个屏幕截图吗?并记录最后100个字符的响应,看看它在哪里像这样:log.d(“,htmltext.substring(htmltext.length()-100));