开发者

Android Web Request Title grab

开发者 https://www.devze.com 2023-04-07 05:15 出处:网络
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

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 S开发者_开发百科tring 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消