1
public class MainActivity extends Activity {

    public static LinearLayout layout = null;
    public static EditText urlstring = null;
    public static Button submit = null;
    public static ListView emailsfound = null;
    public static ArrayList<String> emaillist = new ArrayList<String>();
    public static ArrayList<String> urllist = new ArrayList<String>();
    public static ArrayAdapter<String> adapter = null;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        layout = new LinearLayout(this);
        urlstring = new EditText(this);
        submit = new Button(this);
        submit.setText("Submit");
        emailsfound = new ListView(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(urlstring);
        layout.addView(submit);
        layout.addView(emailsfound);
        setContentView(layout);
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, emaillist);
        emailsfound.setAdapter(adapter);
        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                urllist.add(urlstring.getText().toString());
                int i = 0;
                while (true) {
                    String page = getPage(urlstring.getText().toString());
                    ArrayList<String> urls = new ArrayList<String>();
                    urls = getURLs(page);
                    ArrayList<String> addresses = new ArrayList<String>();
                    addresses = getAddresses(page);
                    for (int a = 0; i < urls.size(); i++) {
                        urllist.add(urls.get(a));
                    }
                    for (int a = 0; a < addresses.size(); i++) {
                        emaillist.add(addresses.get(a));
                    }
                    removeDuplicates(urllist);
                    removeDuplicates(emaillist);
                    adapter.notifyDataSetChanged();
                    i++;
                    urlstring.setText(urllist.get(i).toString());
                }
            }

            ;
        });
    }

    public String getPage(String url) {

        if (url.toLowerCase().startsWith("http") == false) {
            url = "http://" + url;
        }
        URL fromstring = null;
        URLConnection openConnection = null;
        try {
            fromstring = new URL(url);
            openConnection = fromstring.openConnection();
            BufferedReader in = null;
            url = "";
            in = new BufferedReader(new InputStreamReader(openConnection.getInputStream()));
            String input = "";
            if (in != null) {
                while ((input = in.readLine()) != null) {
                    url = url + input + "\r\n";
                }
                in.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return url;
    }

    public ArrayList<String> getURLs(String page) {

        ArrayList<String> s = new ArrayList<String>();
        while (page.toLowerCase().contains("<a href=\"http")) {
            int i = page.toLowerCase().indexOf("href=\"http") + "href=\"".length();
            if (i > -1) {
                String s1 = page.substring(i, page.toLowerCase().indexOf("\"", i));
                s.add(s1);
                page = page.substring(i + "http".length());
            }
        }
        return s;
    }

    public boolean validate(String email) {

        Pattern pattern;
        Matcher matcher;
        final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*" + "@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        pattern = Pattern.compile(EMAIL_PATTERN);
        matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public ArrayList<String> getAddresses(String page) {

        ArrayList<String> s = new ArrayList<String>();
        while (page.contains("@")) {
            int i = page.indexOf("@");
            if (i > -1) {
                int beginning = page.lastIndexOf(" ", i);
                if (beginning > -1) {
                    int ending = page.indexOf(" ", i);
                    if (ending > -1) {
                        String address = page.substring(beginning + 1, ending - 1);
                        address = address.toLowerCase();
                        if (address.startsWith("href=\"mailto:")) {
                            int b = address.indexOf(":") + 1;
                            address = address.substring(b);
                            int e = address.indexOf("\"");
                            if (e > -1) {
                                if (e > address.indexOf("@")) {
                                    address = address.substring(0, address.indexOf("\""));
                                }
                                if (address.contains("?")) {
                                    address = address.substring(0, address.indexOf("?"));
                                }
                                if (!address.contains("<") && !address.contains(">")) {
                                    if (validate(address)) {
                                        s.add(address);
                                    }
                                }
                            } else {
                                int b2 = address.indexOf(">") + 1;
                                if (b2 > -1) {
                                    if (b2 < address.indexOf("@")) {
                                        address = address.substring(b2);
                                    }
                                }
                                int e2 = address.indexOf("<");
                                if (e2 > -1) {
                                    if (e2 > address.indexOf("@")) {
                                        address = address.substring(0, e2);
                                    }
                                }
                                if (!address.contains("<") && !address.contains(">")) {
                                    if (validate(address)) {
                                        s.add(address);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            page = page.substring(i + 1);
        }
        return s;
    }

    public ArrayList<String> removeDuplicates(ArrayList<String> List) {

        ArrayList<String> output = new ArrayList<String>();
        for (int i = 0; i < List.size(); i++) {
            boolean b = false;
            for (int a = 0; a < List.size(); i++) {
                if (List.get(i).toString().toLowerCase().equals(List.get(a).toString().toLowerCase())) {
                    b = true;
                }
            }
            if (b == false) {
                output.add(List.get(i));
            }
        }
        return output;
    }
}

这个程序是在安卓上网本上用 AIDE 制作的。它在按钮单击时崩溃。我想知道我做错了什么。我之前尝试过使用runnables,但它也崩溃了。我是 android 开发的新手,但我精通 Java。我注意到android和Java之间有很多差异。

4

0 回答 0