好的。所以我试图访问一些在线存在的公共记录并将它们导入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();