您能否帮助我抓住此页面上的标题:http://golfnews.no/golfpaatv.php,例如?按标题我的意思是小时scheldule旁边的粗体文本。我需要抓取每个文本然后将其放在设备的屏幕上。那是我的代码:
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;
}
}
- &GT;我创建的函数private String title (String trax)
不够好,因为它只抓取了第一个标题。你能帮助我推理或者更好的功能吗?谢谢。
答案 0 :(得分:0)
这是我第一次做这样的事情......但是欢迎你 顺便说一下,这是最糟糕的做法。
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;
}