Java 为什么Android线程在完成执行之前被终止?
我有一个从互联网上获取数据的线程。它确保正确执行并检索数据。然而,如果我调用一个应该返回数据的方法,它会给我留下null。从这一点我得出了一个结论,即线程是不知何故停止前刚刚鱼翅 代码如下:Java 为什么Android线程在完成执行之前被终止?,java,android,multithreading,android-asynctask,Java,Android,Multithreading,Android Asynctask,我有一个从互联网上获取数据的线程。它确保正确执行并检索数据。然而,如果我调用一个应该返回数据的方法,它会给我留下null。从这一点我得出了一个结论,即线程是不知何故停止前刚刚鱼翅 代码如下: private class getHash extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... params) { String
private class getHash extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String str = null;
try {
// Create a URL for the desired page
URL url = new URL(params[0]);
// Read all the text returned by the server
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
str = in.readLine();
is.close();
isr.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
hash = str; //If I set a global variable here it gets passed without a hitch
return str;
}
@Override
protected void onPostExecute(String result) {
hash = result; // If I comment the line above and live this one I left with a null
}
}
启动异步任务的内容
{
....
getHash hashThread = new getHash(this);
hashThread.execute(new String[] {"http://www.full.path/to/the/file.hash"});
return; // ok now we just have to wait for it to finish ... can't read it until then
}
// Separate callback method
public void onHashComplete(String hash) {
if(hash != null && !hash.equals(localHash)) {
....
}
....
}
现在在您的GetHash类中
public String doInBackground(String[] params) {
.... // don't set hash here ... it will work but you will probably read it at the wrong time.
return str;
}
public void onPostExecute(String str) {
onHashComplete(str); // or just do all the work in here since it is a private inner class
}
希望这能有所帮助。记住
doInBackground()
发生在异步任务线程上,onPostExecute()
在主线程上执行。任何名为execute()
的线程都应该是主线程。由于主线程的工作方式,在它用来调用execute()
的回调完成之前,不能期望onPostCreate()
发生。这就是我添加返回的原因。无论启动异步任务的是什么
{
....
getHash hashThread = new getHash(this);
hashThread.execute(new String[] {"http://www.full.path/to/the/file.hash"});
return; // ok now we just have to wait for it to finish ... can't read it until then
}
// Separate callback method
public void onHashComplete(String hash) {
if(hash != null && !hash.equals(localHash)) {
....
}
....
}
现在在您的GetHash类中
public String doInBackground(String[] params) {
.... // don't set hash here ... it will work but you will probably read it at the wrong time.
return str;
}
public void onPostExecute(String str) {
onHashComplete(str); // or just do all the work in here since it is a private inner class
}
希望这能有所帮助。记住
doInBackground()
发生在异步任务线程上,onPostExecute()
在主线程上执行。任何名为execute()
的线程都应该是主线程。由于主线程的工作方式,在它用来调用execute()
的回调完成之前,不能期望onPostCreate()
发生。这就是我添加返回值的原因。“从中我得出了一个结论,线程在finning之前以某种方式停止了。”--或者,您有一个异常。您如何检索该值以证明它确实在结束之前停止了。没有@Commonware我真的得出了一个结论,我没有查看任何异常。GregGiacovelli如果你能把注意力放在我给出的代码示例上。最后,你会发现两行代码中有一些解释性的注释。你能发布更多的代码吗,特别是关于这些方法的调用?我的猜测是一个时间问题,即您正在异步调用此服务,但可能您试图在它完成之前获得结果?嗯,这不是工作方式。如果(哈希!=null)。。。不应该像那样可靠地工作。它是异步发生的。因此,对execute()的调用不会立即返回。事实上,它不能按您想要的方式工作,因为您必须等待任何方法执行完毕,因为它是UI线程上的当前任务。结果只能在UI线程中的下一个事件上发布。您可以通过调用hashThread.get()来阻止UI线程以检索该哈希,但您可能会得到一个ANR。通常,onPostExecute()会调用代码上的函数来表示代码已完成。“由此我得出结论,线程在结束之前不知何故停止了。”——或者,您遇到了一个异常。您如何检索该值以证明它确实在完成之前停止。没有@commonware我确实得出了一个结论,我没有查看任何异常。GregGiacovelli如果你能把注意力放在我给出的代码示例上。最后,你会发现两行代码中有一些解释性的注释。你能发布更多的代码吗,特别是关于这些方法的调用?我的猜测是一个时间问题,即您正在异步调用此服务,但可能您试图在它完成之前获得结果?嗯,这不是工作方式。如果(哈希!=null)。。。不应该像那样可靠地工作。它是异步发生的。因此,对execute()的调用不会立即返回。事实上,它不能按您想要的方式工作,因为您必须等待任何方法执行完毕,因为它是UI线程上的当前任务。结果只能在UI线程中的下一个事件上发布。您可以通过调用hashThread.get()来阻止UI线程以检索该哈希,但您可能会得到一个ANR。通常,onPostExecute()会调用代码上的函数来表示代码已完成。谢谢您的帮助。我一定是在阅读有关线程的文章时错过了这一部分,然后匆忙进入了开发阶段。谢谢你澄清这一点。我一定是在阅读有关线程的文章时错过了这一部分,然后匆忙进行开发。