android 获取网页指定内容
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<ul class="list_1">
<li><a href="/dt/2153.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/992.html" title="ssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/159.html" title="ssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/30.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/156.html" title="sssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/157.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/158.html" title="ssssssssss" target="_blank">sdf</a></li>
</ul>
</body>
</html>
这样的一个网页源码,我需要获得title和href,android代码怎么写? 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<ul class="list_1">
<li><a href="/dt/2153.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/992.html" title="ssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/159.html" title="ssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/30.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/156.html" title="sssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/157.html" title="sssssssssss" target="_blank">sdf</a></li>
<li><a href="/dt/158.html" title="ssssssssss" target="_blank">sdf</a></li>
</ul>
</body>
</html>
这样的一个网页源码,我需要获得title和href,android代码怎么写? 展开
3个回答
展开全部
android也是用的java语言,使用java中的字符串方法就能实现想要的功能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
演示下如何获取指定网页中的title。
import 略;
/**
* @author Jasonzhou
* @since 2013-03-21
* @功能 读取网页内容
* @说明 获取网页的全部代码后使用jsuop进行处理, 以便得到想要的内容
**/
public class MainActivity extends Activity {
// 指定网页地址
public String url = "http://blog.csdn.net/jasonzhou613/article/details/7905388";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manageException();// 处理抛出异常
TextView tv = (TextView) findViewById(R.id.text);
String myString = null;
myString = posturl(url);
// 设置屏幕显示
Log.i("---网页代码---", "" + myString);
String html = myString;
Document doc = Jsoup.parse(html);
tv.setText(doc.title());
}
/**
* 获取参数指定的网页代码,将其返回给调用者,由调用者对其解析 返回String
*/
public String posturl(String url) {
InputStream is = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
return "Fail to establish http connection!" + e.toString();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
return "Fail to convert net stream!";
}
return result;
}
public void manageException() {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // or
// .detectAll()
// for
// all
// detectable
// problems
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
.build());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
import 略;
/**
* @author Jasonzhou
* @since 2013-03-21
* @功能 读取网页内容
* @说明 获取网页的全部代码后使用jsuop进行处理, 以便得到想要的内容
**/
public class MainActivity extends Activity {
// 指定网页地址
public String url = "http://blog.csdn.net/jasonzhou613/article/details/7905388";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manageException();// 处理抛出异常
TextView tv = (TextView) findViewById(R.id.text);
String myString = null;
myString = posturl(url);
// 设置屏幕显示
Log.i("---网页代码---", "" + myString);
String html = myString;
Document doc = Jsoup.parse(html);
tv.setText(doc.title());
}
/**
* 获取参数指定的网页代码,将其返回给调用者,由调用者对其解析 返回String
*/
public String posturl(String url) {
InputStream is = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
return "Fail to establish http connection!" + e.toString();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
return "Fail to convert net stream!";
}
return result;
}
public void manageException() {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork() // or
// .detectAll()
// for
// all
// detectable
// problems
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath()
.build());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用jsoup.jar 具体可以百度
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |