Android Web请求标题抓取

发布于 2024-12-06 04:59:41 字数 2265 浏览 1 评论 0原文

您能帮我获取此页面的标题吗: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‖放下 2024-12-13 04:59:41

这是我第一次做这样的事情...不过不客气
顺便说一句,这是最糟糕的方法。

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;
}

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文