我正在尝试使用我上一篇文章中建议的方法实现URL重定向处理程序:
How To Capture Session Key Generated From URL Redirector
...但是在尝试这样做时我遇到了3个错误:
Syntax error on token "(", ; expected MainActivity.java /test/src/com/example/test line 68 Java Problem
The method manualRedirectHandler(String) is undefined for the type MainActivity.MyTask MainActivity.java /test/src/com/example/test line 66 Java Problem
Syntax error, insert ";" to complete LocalVariableDeclarationStatement MainActivity.java /test/src/com/example/test line 68 Java Problem
来源:
package com.example.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
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.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;
String url = "http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
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();
}
@Override
protected String doInBackground(String... params) {
ImageView img = (ImageView) findViewById(R.id.imageView1);
{
try {
manualRedirectHandler(url);
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());
}
String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
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);
}
}
}
编辑(第一次回答后):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
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();
}
@Override
protected String doInBackground(String... params) {
ImageView img = (ImageView) findViewById(R.id.imageView1);
{
try {
String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
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;
}
}
void manualRedirectHandler(url);
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 void onPostExecute(String title) {
super.onPostExecute(title);
prog.dismiss();
tv.setText(title);
}
}
}
问题(带编辑):
Syntax error on token "url", VariableDeclaratorId expected after this token MainActivity.java /test/src/com/example/test line 108 Java Problem
url cannot be resolved to a type MainActivity.java /test/src/com/example/test line 108 Java Problem
Type mismatch: cannot convert from Connection.Response to HttpConnection.Response MainActivity.java /test/src/com/example/test line 112 Java Problem
答案 0 :(得分:0)
问题出在这一行:
Document manualRedirectHandler(String url) throws IOException{
您无法在java 中的方法中声明方法,您应该将其移到doInBackground
方法之外。或者在方法doInBackground
中声明一个内部类,当然内部类可以包含manualRedirectHandler
方法。