简体   繁体   English

Callable 不会从类中返回任何内容

[英]Callable doesn't return anything from class

I'm doing an App that scrapes a website an get some images, that works fine, I created an ThreadPoolExecutor and some callable but when I try to get the results from a Callable that I created I'm unnable to, it goes to the ExecutionException.我正在做一个应用程序,它可以抓取网站并获取一些图像,效果很好,我创建了一个 ThreadPoolExecutor 和一些可调用对象,但是当我尝试从我创建的可调用对象中获取结果时,我无法访问,它转到执行异常。

This is the Scraper Class:这是刮板类:

public class ImageScraper implements Callable<String>{

Context context = null;
public Logo logoActivity;
Document[] doc = new Document[1];
List<ImageObject> imageList = new ArrayList<ImageObject>();
int pagAnterior;
String url;
int pag;



public ImageScraper(Logo act, String url, int pag, int pagAnterior) {
    this.logoActivity = act;
    this.url = url;
    this.pag = pag;
    this.pagAnterior = pagAnterior;
    context = act.getApplication();

}

@Override
public String call() throws Exception {
    getResponse(url,pag);
    getImages(doc[0]);
    Log.i("listaaa", "listaa  : "+imageList.size());
    String something = "got something";
    return something;
}


public void getImages(Document docfinal)  {

    Log.i("Documento1", "documento1  : "+docfinal);

    Elements entradas = docfinal.select("img[src]");
    Elements titulo = doc[0].select("title");
    String tituloPagina = titulo.text();
    String urlImage = "";

    if(!tituloPagina.toLowerCase().contains("page "+pagAnterior)) {

        for (Element elem : entradas) {

            if (elem.attr("abs:src").toLowerCase().contains("mfiles")) {
                urlImage = elem.attr("abs:src").replace("thumb-", "");
                Log.i("GridVerticalFragment", "Pillando url: " + urlImage);
                ImageObject image = new ImageObject(urlImage);
                Log.i("GridVerticalFragment", "Url Pillada: " + image.getUrl());
                imageList.add(image);
            }

        }
    }
    Log.i("Logo", "Lista2: "+imageList.size());
}


public void getResponse(String urlRoot, int pagina) {
    Log.i("GridVerticalLayaout", "Pagina: "+pagina);
    String url;
    String urlFinal = "";
    if(pagina==0){
        url = urlRoot;
        urlFinal = url;
    }else{
        url = urlRoot.concat("?page="+Integer.toString(pagina));
        urlFinal = url;
    }

    RequestQueue rq = Volley.newRequestQueue(context);
    Log.i("GridVerticalLayaout", "fuuck: "+url);
    Log.i("GridVerticalLayaout", "lool: "+urlFinal);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, urlFinal,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Do something with the response
                    doc[0] = Jsoup.parse(response);
                    getImages(doc[0]);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Handle error
                    Log.e("ERROR", "Error occurred ", error);
                }
            });
    rq.add(stringRequest );
}

}

And this is the main class:这是主类:

public class Logo extends AppCompatActivity {


public List<ImageObject> GlobalImageList = new ArrayList<ImageObject>() ;
Document[] doc = new Document[1];
String url;
int pagAnterior = 0;
int i = 0;
Context context = null;
public ThreadPoolExecutor executor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logo);
    getSupportActionBar().hide();

    Collection<Callable<String>> callableList = new ArrayList<>();

    context = getApplication();

    int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
    executor = new ThreadPoolExecutor(
            NUMBER_OF_CORES*2,
            NUMBER_OF_CORES*2,
            60L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>()
    );


    while(i<4){
        callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
        i++;
    }

    List<Future<String>> result = null;
    try {
        result = executor.invokeAll(callableList);
    } catch (InterruptedException e) {
        Log.i("Fallo", "Fallo");
        e.printStackTrace();
    }
    for (Future a : result) {
        String SingleImageList = null;

        try {
            SingleImageList = (String) a.get();
            Log.i("Single", "Single"+SingleImageList);
        } catch (InterruptedException e) {
            Log.i("Fallo3", "Fallo3");
            e.printStackTrace();
        } catch (ExecutionException e) {
            Log.i("Fallo2", "Fallo2");
            e.printStackTrace();
        }



}

}


}

This doesn't return anything but the scraper do his job,( the getImages() and getResponse() do the job and updates the list , instead if in this part of the code in MainClass:这不会返回任何内容,但刮刀完成他的工作,( getImages() 和 getResponse() 完成工作并更新列表,而不是在 MainClass 的这部分代码中:

while(i<4){
        callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
        i++;
    }

If I change that for this( the class is not called the callable is created in the class), it works, the string is returned:如果我为此更改它(该类不称为在类中创建的可调用对象),它会起作用,返回字符串:

        while(i<4){
        callableList.add(new Callable<String>() {
            public String call() throws Exception {
                return "haha";
            }
        });
        i++;
    }

Someone can help me with this?有人可以帮我解决这个问题吗? I've been reading a lot and according to what I've read, what I have in the Scraper class is fine, but I still don't get it.我已经阅读了很多,根据我所阅读的内容,我在Scraper类中的内容很好,但我仍然不明白。

Sorry for the bad english, Im trying to :), I don't want to return an string I want to return an ArrayList, im returning a string in the code because thought it was a problem for returning ArrayList, but it seems like that is not that, and again, Thanks!对不起,英语不好,我正在尝试:),我不想返回一个字符串我想返回一个 ArrayList,我在代码中返回一个字符串,因为认为这是返回 ArrayList 的问题,但似乎是这样是不是,再次,谢谢!

Please check this line of code请检查这行代码

while(i<4){
    callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
    i++;
}    

You are not closing the double quotes!你没有关闭双引号! Instead, try this code相反,试试这个代码

while(i<4){
    callableList.add(new ImageScraper(this,"www.google.es",i,i-1););
    i++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM