Android Web请求标题抓取
问题描述:
请帮我抓一下这个页面的标题:http://golfnews.no/golfpaatv.php,例如?标题我的意思是Bold小时Scheldule旁边的文本。我需要抓住每个文本,然后将其放在设备的屏幕上。这是我的代码:Android Web请求标题抓取
package com.work.webrequest;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class WebRequest extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String trax;
String aux = "";
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.textView1);
trax=getPage();
aux=title (trax);
txt.setText(aux);
}
private String title (String trax)
{
String aux = "";
int i,j,n;
n=trax.length();
for(i=1;i<=n;i++)
{
if(trax.charAt(i-1)=='2'&&trax.charAt(i)=='>')
{
break;
}
}
for(j=i+1;j<=n;j++)
{
if(trax.charAt(j)=='<'&&trax.charAt(j+1)=='/')
{
break;
}
}
System.out.println("n ESTE EGAL CU "+n+"i ESTE EGAL CU "+i+" SI j ESTE EGAL CU "+j);
aux = trax.substring(i+1, j);
return aux;
}
private String getPage() {
String str = "***";
try
{
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost("http://golfnews.no/golfpaatv.php");
HttpResponse rp = hc.execute(post);
if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
str = EntityUtils.toString(rp.getEntity());
}
}catch(IOException e){
e.printStackTrace();
}
return str;
}
}
- >在我创建功能,private String title (String trax)
不够好,因为它抓起只有第一个冠军。能否请你帮我推理或许,具有更好的功能?谢谢。
答
这是第一次我会做这样的事......但欢迎您
顺便说一句,这是做到这一点的最糟糕的方式。
private String[] title (String trax)
{
String aux[] = trax.split("program-info");
int n = aux.length;
String[] result = new String[i=1];
for(int i=1;i<=n;i++)
result[i-1] = aux[i].subString(aux[i].indexOf("<h2>")+4,aux[i].indexOf("</h2>"));
return result;
}
它只抓取第一个标题,因为它只有一个字符串。你考虑过一个String数组吗? – Mxyk
是的,我想出了它为什么只抓住第一个标题。如果我使用一个字符串数组,我会为它提供什么维度?标题的数量从一天到另一天不一样。你能帮我一个函数的例子吗? – Teo