KSOAP:当 Web 服务 url 错误时 Android 应用程序崩溃
我正在使用 KSoap 从我的 android 程序访问 .net web 服务。当 Web 服务关闭或 Web 服务 url 错误时,我的应用程序崩溃。
有人可以告诉我一种方法来处理这个问题。
我的代码如下
public String logOut(String prefURL){
String Desc = "logging out";
String NAMESPACE="http://tempuri.org/";
String METHOD_NAME = "Logout";
String SOAP_ACTION = "http://tempuri.org/Logout";
String URL = prefURL+"/Authentication.asmx";
//creating soap object
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
//adding properties to Request Soap Object
Request.addProperty("resourceID", ResourceID);
Request.addProperty("organisationID", OrganisatoinID);
Request.addProperty("description", Desc);
Request.addProperty("sessionID", SessionID);
Request.addProperty("imei", LoginActivity.deviceid);
Request.addProperty("longtitude", String.valueOf(LoginActivity.RDMSLon));
Request.addProperty("langtitude", String.valueOf(LoginActivity.RDMSLat));
Request.addProperty("deviceType", 4);
//creating soap envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(Request);
//creating transport object
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultString = (SoapPrimitive)envelope.getResponse();
//jason array stored into a string
jstringarray = String.valueOf(resultString);
Log.v(TAG, "Logout" + jstringarray);
jArray = new JSONArray(String.valueOf(jstringarray));
JSONObject json_data = null;
json_data = jArray.getJSONObject(0);
Status = json_data.getString("Status");
Log.v(TAG, "Logout: "+ Status);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return Status;
}
logcat extract
10-26 16:39:39.174: V/Login2(791): Inside preexecute
10-26 16:39:40.204: W/System.err(791): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <HTML>@1:6 in java.io.InputStreamReader@44f1f8e8)
10-26 16:39:40.248: W/System.err(791): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
10-26 16:39:40.254: W/System.err(791): at org.kxml2.io.KXmlParser.require(KXmlParser.java:1431)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
10-26 16:39:40.254: W/System.err(791): at com.eyepax.rdms.service.ValidateUser.validate(ValidateUser.java:69)
10-26 16:39:40.254: W/System.err(791): at com.eyepax.rdms.LoginActivity$LoginTask.doInBackground(LoginActivity.java:434)
10-26 16:39:40.265: W/System.err(791): at com.eyepax.rdms.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
10-26 16:39:40.265: W/System.err(791): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-26 16:39:40.265: W/System.err(791): at java.lang.Thread.run(Thread.java:1096)
10-26 16:39:40.265: V/validate_user(791): Excepion
10-26 16:39:40.265: V/Login2(791): Inside doingback
10-26 16:39:40.265: V/Login2(791): Inside onPost
10-26 16:39:40.265: D/AndroidRuntime(791): Shutting down VM
10-26 16:39:40.265: W/dalvikvm(791): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-26 16:39:40.284: E/AndroidRuntime(791): FATAL EXCEPTION: main
10-26 16:39:40.284: E/AndroidRuntime(791): java.lang.NullPointerException
10-26 16:39:40.284: E/AndroidRuntime(791): at com.eyepax.rdms.LoginActivity$LoginTask.onPostExecute(LoginActivity.java:443)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.eyepax.rdms.LoginActivity$LoginTask.onPostExecute(LoginActivity.java:1)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:123)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-26 16:39:40.284: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 16:39:40.284: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:521)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-26 16:39:40.284: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)
提前致谢。
I'm using KSoap to acess .net webservice from my android programme. When the webservice is either down or webservice url is wrong my application crashes.
Can someone tell me a way to handle this issue.
My code goes as below
public String logOut(String prefURL){
String Desc = "logging out";
String NAMESPACE="http://tempuri.org/";
String METHOD_NAME = "Logout";
String SOAP_ACTION = "http://tempuri.org/Logout";
String URL = prefURL+"/Authentication.asmx";
//creating soap object
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
//adding properties to Request Soap Object
Request.addProperty("resourceID", ResourceID);
Request.addProperty("organisationID", OrganisatoinID);
Request.addProperty("description", Desc);
Request.addProperty("sessionID", SessionID);
Request.addProperty("imei", LoginActivity.deviceid);
Request.addProperty("longtitude", String.valueOf(LoginActivity.RDMSLon));
Request.addProperty("langtitude", String.valueOf(LoginActivity.RDMSLat));
Request.addProperty("deviceType", 4);
//creating soap envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(Request);
//creating transport object
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultString = (SoapPrimitive)envelope.getResponse();
//jason array stored into a string
jstringarray = String.valueOf(resultString);
Log.v(TAG, "Logout" + jstringarray);
jArray = new JSONArray(String.valueOf(jstringarray));
JSONObject json_data = null;
json_data = jArray.getJSONObject(0);
Status = json_data.getString("Status");
Log.v(TAG, "Logout: "+ Status);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return Status;
}
logcat extract
10-26 16:39:39.174: V/Login2(791): Inside preexecute
10-26 16:39:40.204: W/System.err(791): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <HTML>@1:6 in java.io.InputStreamReader@44f1f8e8)
10-26 16:39:40.248: W/System.err(791): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
10-26 16:39:40.254: W/System.err(791): at org.kxml2.io.KXmlParser.require(KXmlParser.java:1431)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
10-26 16:39:40.254: W/System.err(791): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
10-26 16:39:40.254: W/System.err(791): at com.eyepax.rdms.service.ValidateUser.validate(ValidateUser.java:69)
10-26 16:39:40.254: W/System.err(791): at com.eyepax.rdms.LoginActivity$LoginTask.doInBackground(LoginActivity.java:434)
10-26 16:39:40.265: W/System.err(791): at com.eyepax.rdms.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
10-26 16:39:40.265: W/System.err(791): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-26 16:39:40.265: W/System.err(791): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-26 16:39:40.265: W/System.err(791): at java.lang.Thread.run(Thread.java:1096)
10-26 16:39:40.265: V/validate_user(791): Excepion
10-26 16:39:40.265: V/Login2(791): Inside doingback
10-26 16:39:40.265: V/Login2(791): Inside onPost
10-26 16:39:40.265: D/AndroidRuntime(791): Shutting down VM
10-26 16:39:40.265: W/dalvikvm(791): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-26 16:39:40.284: E/AndroidRuntime(791): FATAL EXCEPTION: main
10-26 16:39:40.284: E/AndroidRuntime(791): java.lang.NullPointerException
10-26 16:39:40.284: E/AndroidRuntime(791): at com.eyepax.rdms.LoginActivity$LoginTask.onPostExecute(LoginActivity.java:443)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.eyepax.rdms.LoginActivity$LoginTask.onPostExecute(LoginActivity.java:1)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask.finish(AsyncTask.java:417)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.os.Looper.loop(Looper.java:123)
10-26 16:39:40.284: E/AndroidRuntime(791): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-26 16:39:40.284: E/AndroidRuntime(791): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 16:39:40.284: E/AndroidRuntime(791): at java.lang.reflect.Method.invoke(Method.java:521)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-26 16:39:40.284: E/AndroidRuntime(791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-26 16:39:40.284: E/AndroidRuntime(791): at dalvik.system.NativeStart.main(Native Method)
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了问题。当异常发生时,没有消息返回到主线程。我在异常块内添加了一个字符串值“失败”,我向变量添加了一个值。所以不再有空指针。
Figured out the issue. When exception occured there are no message to be returned to main thread. I added a string value "failed" inside excepton block I added a value to the variable. so no more null pointer.