0

I'm attempting to implement a URL redirect handler using a method suggested in a previous post of mine:

How To Capture Session Key Generated From URL Redirector

...however I'm getting 3 errors when attempting to do so:

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

SOURCE:

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);

        }

    }
}

EDIT (after first answer):

    @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);

    }

}

}

ISSUES (with edit):

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
4

1 回答 1

0

问题出在这一行:

Document manualRedirectHandler(String url) throws IOException{

您不能在 java 中的方法内声明方法,您应该将其移到doInBackground方法之外。或者在方法中声明一个内部类doInBackground,当然该内部类可以包含该manualRedirectHandler方法。

于 2013-10-09T20:39:52.883 回答