[英]Calling SeekBar.getProgress(); from another class doesn't return anything?
[英]Callable doesn't return anything from class
我正在做一個應用程序,它可以抓取網站並獲取一些圖像,效果很好,我創建了一個 ThreadPoolExecutor 和一些可調用對象,但是當我嘗試從我創建的可調用對象中獲取結果時,我無法訪問,它轉到執行異常。
這是刮板類:
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 );
}
}
這是主類:
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();
}
}
}
}
這不會返回任何內容,但刮刀完成他的工作,( getImages() 和 getResponse() 完成工作並更新列表,而不是在 MainClass 的這部分代碼中:
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
i++;
}
如果我為此更改它(該類不稱為在類中創建的可調用對象),它會起作用,返回字符串:
while(i<4){
callableList.add(new Callable<String>() {
public String call() throws Exception {
return "haha";
}
});
i++;
}
有人可以幫我解決這個問題嗎? 我已經閱讀了很多,根據我所閱讀的內容,我在Scraper類中的內容很好,但我仍然不明白。
對不起,英語不好,我正在嘗試:),我不想返回一個字符串我想返回一個 ArrayList,我在代碼中返回一個字符串,因為認為這是返回 ArrayList 的問題,但似乎是這樣是不是,再次,謝謝!
請檢查這行代碼
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
i++;
}
你沒有關閉雙引號! 相反,試試這個代碼
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es",i,i-1););
i++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.