KSOAP:当webservice url出错时,Android应用程序崩溃

时间:2011-10-26 10:27:21

标签: android web-services ksoap

我正在使用KSoap从我的android程序访问.net webservice。当webservice关闭或webservice 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)

提前致谢。

1 个答案:

答案 0 :(得分:0)

找出问题所在。发生异常时,没有消息要返回主线程。我在里面添加了一个字符串值“failed”,除了块我向变量添加了一个值。所以没有更多的空指针。