0

Ok。だから私は、オンラインに存在するいくつかの公的記録にアクセスしてAndroidにインポートしようとしています。それらは現在、スクレイピングしたい実際のデータを含む別のページへのリンクを含む jSoup を使用してアプリにプルするテーブルを含む HTML ページを返す、公開されている Web サービスとして存在します。これまでのところ、最初の 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();
4

1 に答える 1