在Java / Android中从重定向的URL捕获表数据

时间:2013-10-16 16:32:47

标签: java android response.redirect http-redirect url-redirection

确定。所以我试图访问一些在线存在的公共记录并将它们导入Android。它们目前作为公共可用的Web服务存在,它返回一个HTML页面,其中包含一个表格,我使用jSoup将其拉入我的应用程序,其中包含指向另一个页面的链接,其中包含我想要抓取的实际数据。到目前为止,我已经构建了一个刮刀,它可以从第一个HTML页面中删除数据,从表中提取并将其显示为textview01。我需要做的就是按照该链接并在textview02中显示重定向页面的数据。

我知道这既简单又困难 - 任何帮助都非常感谢!

package com.example.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView tv;
    TextView tv2;
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=AARON&fname=";
    String tr;
    Document doc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        tv = (TextView) findViewById(R.id.TextView02);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

        }

                   private Document manualRedirectHandler(String url) throws IOException{
                        Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
                        int status = response.statusCode();

                    if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
                    {
                        String redirectUrl = response.header("location");
                        System.out.println("Redirect to: " + redirectUrl);//key will be here
                        return manualRedirectHandler(redirectUrl);
                    }

                    return Jsoup.parse(response.body());
                    }

        @Override
        protected String doInBackground(String... params) {

            ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {


                    manualRedirectHandler(url);

                    String imageURL = "http://exampleurl.com/icon.jpg"; // this needs to display the user's image from the redirected page
                    HttpGet httpRequest = null;

                    httpRequest = new HttpGet(URI.create(imageURL));

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

                    Bitmap bitmap = BitmapFactory.decodeStream(input);

                    img.setImageBitmap(bitmap);

                    doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

                    Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);
           // tv2.setText(title2); - this needs work - I need to update this textview with the table data from the URL obtained after redirect

        }

    }
}

第一反应后的编辑/答案:

public class MainActivity extends Activity {

    TextView tv;
    TextView tv2;
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=hello&fname=";
    String tr;
     Document doc = Jsoup.connect(url).data("lname", "LastNameFromUser").data("firstname","fname").post();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.TextView01);
        tv = (TextView) findViewById(R.id.TextView02);
        new MyTask().execute(url);
    }

    private class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog prog;

        String title = "";

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(MainActivity.this);
            prog.setMessage("Loading....");
            prog.show();

        }

                   private Document manualRedirectHandler(String url) throws IOException{
                        Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
                        int status = response.statusCode();

                    if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
                    {
                        String redirectUrl = response.header("location");
                        System.out.println("Redirect to: " + redirectUrl);//key will be here
                        return manualRedirectHandler(redirectUrl);
                    }

                    return Jsoup.parse(response.body());
                    }

        @Override
        protected String doInBackground(String... params) {

            ImageView img = (ImageView) findViewById(R.id.imageView1);
            {
                try {


                    manualRedirectHandler(url);

                    String imageURL = "http://exampleurl.com/icon.jpg"; // this needs to display the user's image from the redirected page
                    HttpGet httpRequest = null;

                    httpRequest = new HttpGet(URI.create(imageURL));

                    HttpClient httpclient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpclient
                            .execute(httpRequest);

                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                    InputStream input = b_entity.getContent();

                    Bitmap bitmap = BitmapFactory.decodeStream(input);

                    img.setImageBitmap(bitmap);

                    doc = Jsoup.connect(params[0]).get();
                    Element tableElement = doc.select(".datagrid").first();

                    Elements tableRows = tableElement.select("tr");
                    for (Element row : tableRows) {
                        Elements cells = row.select("td");
                        if (cells.size() > 0) {
                            title = cells.get(0).child(0).attr("href") + " ; "
                                    + cells.get(0).text() + "; "
                                    + cells.get(1).text() + "; "
                                    + cells.get(2).text() + "; "
                                    + cells.get(3).text();
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return title;
            }
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            prog.dismiss();
            tv.setText(title);
           // tv2.setText(title2); - this needs work - I need to update this textview with the table data from the URL obtained after redirect

        }

    }
}

结果:

“默认构造函数无法处理由隐式超级构造函数抛出的异常类型IOException。必须在行上定义一个显式构造函数:

Document doc = Jsoup.connect(url).data("lname", "LastNameFromUser").data("firstname","fname").post();

1 个答案:

答案 0 :(得分:0)

我在这里创建了类似的内容:https://play.google.com/store/apps/details?id=com.search.wisconsinccap&hl=en

要做到这一点,你需要做类似的事情。

/*Assuming you are using post()*/
    String url = "http://sheriff.org/apps/arrest/results.cfm?lname=hello&fname=";
    Document doc = Jsoup.connect(url).data("lname", "LastNameFromUser").data("fname","FirstNameFromUser").post();

EDIT 重试上面的代码片段。您也可以只构建URL,然后连接到该URL。

此代码正在运作:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class SheriffParser {
    private String firstName;
    private String lastName;
    public static final String URL = "http://sheriff.org/apps/arrest/results.cfm?";

    public SheriffParser(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Elements fillOutForm() throws IOException {
        return Jsoup.connect(this.URL).data("lname", this.lastName)
                .data("fname", this.firstName).post().select("tbody");
    }

public static void main(String[] args) throws IOException {
    String url = "http://sheriff.org/apps/arrest/results.cfm?";
    SheriffParser parser = new SheriffParser("InsertFirstName","InsertLastName");
    System.out.println(parser.fillOutForm().toString());
}

}

这将为您提供表格,然后您使用解析算法来分隔类别。您应该创建另一个类,其中包含Arrest No.,Name,Sex,Location as Parameters。

请考虑将您的Activity分解为多个类并使用更好的命名约定。这将使您能够更好地诊断错误,并使代码更具可读性。