下面的代码通常运行良好,但是,如果代码正在执行且连接突然丢失,则经常会发生错误。我在这个问题的底部报告的错误似乎是从430行附近开始的,即:
androidHttpTransport.call(soap_action, envelope);
以下是完成我工作的AsyncTask:
private class CheckUpdateTask extends AsyncTask<Void, Void, ArrayList<String>>{
private SoapObject response = null;
ArrayList<String> methodsResult = new ArrayList<String>();
@Override
protected ArrayList<String> doInBackground(Void... params) {
// TODO Auto-generated method stub
SoapObject Request = new SoapObject(NAMESPACE,"CheckUpdate");
Request.addProperty("Username", username);
Request.addProperty("Password", password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
final String soap_action = NAMESPACE+"CheckUpdate";
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(soap_action, envelope); //line 430, where error occurs!
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
try {
response = (SoapObject)envelope.getResponse();
...here I use the returned data for build my array (methodsResult)
SoapObject pi = (SoapObject)response.getProperty(0);
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return methodsResult;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// TODO Auto-generated method stub
String[] resultArr= result.toArray(new String[result.size()]);
new callWS().execute(resultArr);
super.onPostExecute(result);
}
}
这是我的错误信息:
05-07 12:58:47.920: E/AndroidRuntime(31053): FATAL EXCEPTION: AsyncTask #5
05-07 12:58:47.920: E/AndroidRuntime(31053): java.lang.RuntimeException: An error occured while executing doInBackground()
05-07 12:58:47.920: E/AndroidRuntime(31053): at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-07 12:58:47.920: E/AndroidRuntime(31053): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.lang.Thread.run(Thread.java:856)
05-07 12:58:47.920: E/AndroidRuntime(31053): Caused by: java.lang.NullPointerException
05-07 12:58:47.920: E/AndroidRuntime(31053): at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:85)
05-07 12:58:47.920: E/AndroidRuntime(31053): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:167)
05-07 12:58:47.920: E/AndroidRuntime(31053): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
05-07 12:58:47.920: E/AndroidRuntime(31053): at host.framework.ServicePromemoria$CheckUpdateTask.doInBackground(ServicePromemoria.java:430)
05-07 12:58:47.920: E/AndroidRuntime(31053): at host.framework.ServicePromemoria$CheckUpdateTask.doInBackground(ServicePromemoria.java:1)
05-07 12:58:47.920: E/AndroidRuntime(31053): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-07 12:58:47.920: E/AndroidRuntime(31053): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-07 12:58:47.920: E/AndroidRuntime(31053): ... 5 more
我不确定问题是否与连接泄漏有关。
答案 0 :(得分:1)
检查没有变量设置为null。如果没有,问题是http调用只是处理异常。
try {
androidHttpTransport.call(soap_action, envelope); //line 430, where error occurs!
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
enter code here
}
希望它有所帮助!