Android Web请求标题抓取
您能帮我获取此页面的标题吗:http://golfnews.no/golfpaatv.php , 例如 ?我所说的标题是指小时时间表旁边的粗体文本。我需要抓取每个文本,然后将其放在设备的屏幕上。这是我的代码:
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)
不够好,因为它只获取第一个标题..您能否帮助我进行推理,或者也许提供更好的函数?谢谢。
Could you please help me grab the titles from this page : http://golfnews.no/golfpaatv.php , for example ? By titles I mean the Bold text next to the hour scheldule . I need to grab each text and then put it on the device's screen. That's my code :
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;
}
}
-> the function that I created , private String title (String trax)
is not good enough , because it grabs only the first title .. Could you please help me with the reasoning or perhaps , with a better function ? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我第一次做这样的事情...不过不客气
顺便说一句,这是最糟糕的方法。
This is the first time I would do such a thing... But you are welcome
By the way, this is the worst way to do this.