[英]Android - getting null response in AsyncTask
I wrote a XMLParser myself to get a certain string from a xml, everything works fine when testing it in a Java application, but when doing it in android it returns a null value. 我自己编写了XMLParser来从xml中获取某个字符串,在Java应用程序中对其进行测试时,一切正常,但是在android中进行操作时,它返回空值。 I researched and found out that I will need to use AsyncTask in order to call the web service in a separate thread.
我研究发现,我将需要使用AsyncTask才能在单独的线程中调用Web服务。 I did and I still get a null value.
我做到了,但我仍然得到一个空值。 Here is the code
这是代码
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
@Override
protected String doInBackground(String... params) {
//publishProgress("Sleeping..."); // Calls onProgressUpdate()
XmlParser getP = new XmlParser();
resp = getP.getClo();
return resp;
}
@Override
protected void onPostExecute(String result) {
Log.d("Verify: ", "" + result);
}
}
I execute this by making an OnClick on a dialog interface : new AsyncTaskRunner().execute(); 我通过在对话框界面上执行OnClick来执行此操作:new AsyncTaskRunner()。execute(); Thanks!
谢谢!
below piece of code obviously return null. 下面的代码显然返回null。
XmlParser getP = new XmlParser();
resp = getP.getClo();
return resp;
since XmlParser getP = new XmlParser();
由于
XmlParser getP = new XmlParser();
object creation happening, and your trying to access threr only with no value set that object 对象创建正在发生,并且您尝试仅在没有设置该对象值的情况下访问
In your statement, XmlParser getP = new XmlParser();
在您的语句中,
XmlParser getP = new XmlParser();
you have only created an XmlParser, you have yet to set it to a value. 您仅创建了XmlParser,但尚未将其设置为值。 It is effectively useless until you do something with it.
除非您对其进行处理,否则它实际上是无用的。 for more information on the XmlParser see this documentation - http://groovy.codehaus.org/api/groovy/util/XmlParser.html
关于XmlParser的更多信息,请参阅本文档- http://groovy.codehaus.org/api/groovy/util/XmlParser.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.