gpt4 book ai didi

java - AsyncTask android - ExecutionException 错误

转载 作者:行者123 更新时间:2023-12-01 17:16:11 25 4
gpt4 key购买 nike

我的主类中有这段代码:

  public class vurlDownloader extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://url/video.html");
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
} catch (IOException e) {
e.printStackTrace();
}
String result = "";

BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
} catch (IOException e) {
e.printStackTrace();
}


String line = null;
try {
while ((line = reader.readLine()) != null){
result += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
}

return result;

}

}

public String loadVurl(){
String output = new vurlDownloader().execute().get();
return output;
}

在这一行
字符串输出 = new vurlDownloader().execute().get();
Android Studio 给了我奇怪的引用(红色下划线):未处理的异常:java.lang.InterruptedException、java.util.concurrent.Execution.Exception

我不太明白这一点,因为我遇到了类似的情况:How to get a string back from AsyncTask?对于这个人来说它是有效的。

您好!

最佳答案

get() 抛出 java.lang.InterruptedException。您需要 try catch 并捕获那些

http://developer.android.com/reference/android/os/AsyncTask.html#get()

public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

Returns
The computed result.
Throws
CancellationException If the computation was cancelled.
ExecutionException If the computation threw an exception
InterruptedException If the current thread was interrupted while waiting.

但是您不应该使用 get 因为它会阻塞等待结果的 ui 线程。这使得 AsyncTask 不再是异步的。

解决方案

 new vurlDownloader().execute()

如果 Asynctask 是内部类,或者使用接口(interface)作为 Activity 的回调,您可以在 onPostExecute 中更新 ui。

检查blackbel的回答@

How do I return a boolean from AsyncTask?

建议:遵循 java 命名约定,将 vurlDownloader() 重命名为 VurlDownloader()。类名以大写字母开头

编辑:

调用

 vurlDownloader().execute();

然后

class vurlDownloader() extends AsyncTask<Void,Void,String>
{

@Override
protected String doInBackground(Void... params) {

String _response= null;

try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://informat.net.pl/krachapp/video.html");
HttpResponse response = null;
response = httpClient.execute(httpGet, localContext);
_response = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
e.printStackTrace();
}



return _response;
}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i("Result is",result);
}

}

关于java - AsyncTask android - ExecutionException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22010883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com