如何从URL得到HTML源$ C ​​$ c在Android的? [英] How to get HTML source code from url in android?

查看:111
本文介绍了如何从URL得到HTML源$ C ​​$ c在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目中使用DOM解析器来获得源$ C ​​$页面C至获取html源代码code在字符串在vb.net。

I am working on project to get html source code in a string in vb.net using dom parser to get source code of a page.

1)我要实现机器人一样,会有怎样的方式通过调用Android的一个url获得源$ C ​​$网页℃。 2)我将有两个布局源$ C ​​$ C上的布局和其他的网页本身。如果我改变源$ C ​​$ C布局标题标签的价值,它必须自动更新,实际网页?

1) I want to implement the same in android, what will be the approach to get source code of webpage by calling a url in android. 2) I would be having two layouts for source code in one layout and other for the webpage itself. If i am changing title tag value in source code layout, its must be automatically updated on actual webpage ?

什么是做在Android上最好的方法?

What would be the best approach to do that in android ?

任何形式的帮助将是非常美联社preciated。

Any kind of help will be highly appreciated.

推荐答案

为了从您可以使用这样的一个网址让你的源$ C ​​$ C:

In order to get your source code from an URL you can use this :

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
    sb.append(line + "\n");

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

is.close(); // Close the stream

您还可以添加一些PARAMS到HttpClient的管理超时和其他类似的东西。 例如:

You can also add some params to the HttpClient to manage timeout and other stuff like that. Ex :

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,3000); // 3s max for connection
HttpConnectionParams.setSoTimeout(httpParameters, 4000); // 4s max to get data
HttpClient httpclient = new DefaultHttpClient(httpParameters);

这篇关于如何从URL得到HTML源$ C ​​$ c在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆