0

My code to access wcf web service is not working in android app:

on the top I have:

private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.86.164.102/ABCWcfService/ABCWcfService.svc";
private static final String SOAP_ACTION = "http://tempuri.org/IABCWcfService/GetTitle";
private static final String METHOD_NAME = "GetTitle";

then in the method I have:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;

try
{
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    String receivedString = result.toString();
}
catch (Exception e)
{
}

I found it breaks into exception after line androidHttpTransport.call(SOAP_ACTION, envelope); but the e has value null.

I use the same code in a Java project to access the same wcf web service, and it works fine.

I did add:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-sdk android:minSdkVersion="8" />
<application>
     ....
</application>

in the manifest file, before the application section. But still does not work.

I have read a lot of different threads over the internet. None of them solve my problem. Anyone got better suggestion? Thank you.

The web service is running on the same machine as the android app.

Xiao Han
  • 1,004
  • 7
  • 16
  • 33
  • this web request is in `background` thread? – Mohsin Naeem Jul 19 '12 at 03:43
  • What do you mean? It is hosted in local box IIS. I guess it is running in background. – Xiao Han Jul 19 '12 at 04:44
  • I am talking about the app. Above method is in background thread? Where you are getting the `Exception`...What android `OS` you are running? – Mohsin Naeem Jul 19 '12 at 05:06
  • I run the app via debug mode in eclipse, and step through each line of code to androidHttpTransport.call(SOAP_ACTION, envelope); after I press F8(continue) again, it goes into catch exception, and I can see the exception has value e=null – Xiao Han Jul 19 '12 at 06:08
  • Seriously you are not getting my point..may be I am not able to ask a correct question.:) please read this http://developer.android.com/reference/android/os/AsyncTask.html your this code `try { androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive result = (SoapPrimitive) envelope.getResponse(); String receivedString = result.toString(); } catch (Exception e) { }` should be in the `doInBackground` of `AsyncTask` – Mohsin Naeem Jul 19 '12 at 06:23
  • I didn't know I need to call web service at background thread. Cause all the examples I found on the web are using in the main thread, e.g.: http://stackoverflow.com/questions/5891279/runtime-error-when-getresponse-using-android-ksoap?rq=1 and because the contents I get from web service will be display in the UI, so I am not sure if I still need to request in background thread in my case? – Xiao Han Jul 19 '12 at 11:00
  • btw on which android OS you are testing? – Mohsin Naeem Jul 19 '12 at 11:16
  • The AVD is on android2.2 – Xiao Han Jul 19 '12 at 22:06
  • Actually there is one statement which is exceptionMessage = e.toString() inside the catch block. I saw null value from there, not at line catch(Exception e) – Xiao Han Jul 20 '12 at 07:12

1 Answers1

0

I'm still a bit puzzled why your exception is null. Have you tried this on a device or only in the emulator?

An explanation for why the Exception e may be null can be explained in this question about an Exception always null. Short answer: the Emulator may not understand the exception it's receiving and might be substituting a null for the actual exception.

You said the android app is on the same machine as the web service so I read that as you're running this in an emulator, right? Try running this on an actual device and see if something useful is in the device logging.

Log.e(TAG, "caught exception " + e);

It will be a lot easier to solve this if you can get a stacktrace.

The issue about using a network call on your main thread is addressed by a link in that question. Basically it's bad practice to make network calls from the main thread since it affects application responsiveness. For some SDK versions (API Level greater than 10, i.e. Honeycomb and up) a NetworkOnMainThreadException will be thrown and the emulator might not understand that, thus substituting a null.

However, the response to that question may not fully explain what you're seeing since your value for <uses-sdk> lists a minSdkVersion of 8. (no targetSdkVersion but it should still be fine.)

Let's see if we can find out more on this if you can get a stacktrace.

Community
  • 1
  • 1
dustmachine
  • 10,622
  • 5
  • 26
  • 28
  • I only tried this on emulator. something I don't understand is, exactly the same code, working for normal Java project, but not android project in eclipse. – Xiao Han Jul 21 '12 at 06:18
  • Remember that Android is not exactly the same as Java. It is a different virtual machine that runs the code on an Android device (i.e., not the Oracle/Sun VM). The Java class files for your application are converted so they can run on the Dalvik VM and it's _that_ VM which can then enforce additional restrictions on an executable (restrictions such as preventing network access code on the main thread). – dustmachine Jul 23 '12 at 14:38