在Asynctask

时间:2015-07-10 21:03:56

标签: java android jsoup

请耐心等待我,我是Android开发的初学者。 我试图将URL放在ArrayList中并将ArrayList返回给MainActivity。

我可以在Connection类中更新它,但是当我尝试检查它是否在getValidURL()方法中工作时,它不会显示任何内容。

在MainActivity中,我还使用Log.d来记录返回的ArrayList是否为空,并且它是。

我做错了什么?我觉得我犯了一个非常愚蠢的错误,我不知道它是什么。

MainActivity.urlEnteredButton():

public void urlEnteredButton(View view) throws IOException {

    Button ok = (Button) findViewById(R.id.urlButtonOK);

    Intent displayArticleScreen = new Intent(this, SecondActivity.class);

    GetURLS work = new GetURLS();

    ArrayList<String> URLS = work.getValidURL();

    if(URLS.isEmpty()){
        Log.d("URL IN MAIN IS: " , "EMPTY!!!!!");
    }
        for(String s : URLS){
            Log.d("URL Main: " , s);
        }

    if(!URLS.isEmpty()) {
        String[] workingURLS = URLS.toArray(new String[URLS.size()]);
        final int result = 1;

        displayArticleScreen.putExtra("url", workingURLS);

        startActivity(displayArticleScreen);
    }
}

GetURLS.java

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class GetURLS {

    ArrayList<String> workingURLS = new ArrayList<String>();
    Connection myConnection = new Connection();

    public GetURLS() throws IOException {

        myConnection.execute();

    }

    public void update(ArrayList<String> temp){

        for(String s : temp){
            workingURLS.add(s);
        }

        for(String s : workingURLS){
            Log.d("URL in workingURLS", s);
        }

    }

    public ArrayList<String> getValidURL(){
        for(String s : workingURLS){
            Log.d("URL in getValidURL()", s);
        }
        return workingURLS;

    }

    private boolean isValidURL(String temp){
        if(temp.contains("thestar.com") && temp.length() > 75 && (temp.contains("/news/") || temp.contains("/business/") || temp.contains("/sports/") || temp.contains("/entertainment/") || temp.contains("/life/"))){
            return true;
        }
        return false;
    }

    private class Connection extends AsyncTask<Void , Void, ArrayList<String>> {

        ArrayList<String> tempAL = new ArrayList<String>();

        @Override
        protected ArrayList<String> doInBackground(Void... params){
            Document doc = null;
            Elements links = null;


            try {
                doc = Jsoup.connect("http://www.thestar.com/").get();
                links = doc.select("a");
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (links != null) {
                for(Element link : links){
                    String temp = link.attr("href");
                    if(isValidURL(temp)){
                        tempAL.add(temp);
                    }
                }
            }

            return tempAL;

        }

        @Override
        protected void onPostExecute(ArrayList<String> tempAL){

            update(tempAL);

        }

    }

}

在logcat中,我收到以下消息:

07-10 16:41:58.824  21413-21413/com.bloopbloop.ishyfishyy.thestargrabber D/URL IN MAIN IS:﹕ EMPTY!!!!!

07-10 16:41:59.894  21413-21413/com.bloopbloop.ishyfishyy.thestargrabber D/URL in workingURLS﹕ http://www.thestar.com/news/crime/2015/07/10/lecent-rosss-mother-too-grief-stricken-to-speak.html
//followed by all the other URLS I requested
  • 在MainActivity中,为什么if(URLS.isEmpty())根据时间戳在ArrayList<String> URLS = work.getValidURL();之前执行?

  • 为什么GetURLS类中的ArrayList会更新?

提前致谢

1 个答案:

答案 0 :(得分:0)

由于Node version: v0.10.25 Cordova version: 5.0.0 Config.xml file: <?xml version="1.0" encoding="UTF-8"?> <!-- config.xml reference: https://build.phonegap.com/docs/config-xml --> <widget xmlns = "http://www.w3.org/ns/widgets" xmlns:gap = "http://phonegap.com/ns/1.0" 正在启动AsyncTask,因此它会在后台同时发生。因此,即使您所有的网络呼叫都没有完成,执行仍会继续超过MainActivity中的执行。这就是为什么你的日志将ArrayList显示为空的原因:在执行它的时候,它是空的。只有在此之后,网络中的线程才会完成填充,并且会调用myConnectiont.execute()方法。

相关问题