Search code examples
androidweb-servicesksoap

KSOAP: Android Application crashes when the webservice url is wrong


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.


Solution

  • 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.