开发者

Can't connect to WCF service on Android

开发者 https://www.devze.com 2022-12-14 12:55 出处:网络
I am trying to connect to a .NET WCF service in Android using kSOAP2 (v 2.1.2), but I keep getting a fatal exception whenever I try to make the service call. I\'m having a bit of difficulty tracking d

I am trying to connect to a .NET WCF service in Android using kSOAP2 (v 2.1.2), but I keep getting a fatal exception whenever I try to make the service call. I'm having a bit of difficulty tracking down the error and can't seem to figure out why it's happening. The code I am using is below:

package org.example.android;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

public class ValidateUser extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/mobile/ValidateUser";
    private static final String METHOD_NAME = "ValidateUser";
    private static final String NAMESPACE = "http://tempuri.org/mobile/";    
    private static final String URL = "http://192.168.1.2:8002/WebService.Mobile.svc"; 

     /** Called when the activity is first created. */  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.validate_user);

        Intent intent = getIntent();

        Bundle extras = intent.getExtras();

        if (extras == null) {
            this.finish();
        }
        String username = extras.getString("username");
        String password = extras.getString("password");                               

        Boolean validUser = false;


        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo uName = new PropertyInfo();
            uName.name = "userName";

            PropertyInfo pWord = new PropertyInfo();
            pWord.name = "passWord";

            request.addProperty(uName,开发者_运维知识库 username);
            request.addProperty(pWord, password);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransport androidHttpTransport = new HttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope); // error occurs here

            Integer userId = (Integer)envelope.getResponse();
            validUser = (userId != 0);
        } catch (Exception ex) {

        }               
    }

    private void exit () {
        this.finish();
    }
}

EDIT: Remove the old stack traces. In summary, the first problem was the program being unable to open a connection due to missing methods or libraries due to using vanilla kSOAP2 rather than a modified library for Android (kSOAP2-Android). The second issue was a settings issue. In the Manifest I did not add the following setting:

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

I am now having an issue with the XMLPullParser which I need to figure out.

12-23 10:58:06.480: ERROR/SOCKETLOG(210): add_recv_stats recv 0

12-23 10:58:06.710: WARN/System.err(210): org.xmlpull.v1.XmlPullParserException: unexpected type (position:END_DOCUMENT null@1:0 in java.io.InputStreamReader@433fb070)

12-23 10:58:06.710: WARN/System.err(210): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:243)

12-23 10:58:06.720: WARN/System.err(210): at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:1363)

12-23 10:58:06.720: WARN/System.err(210): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:126)

12-23 10:58:06.720: WARN/System.err(210): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)

12-23 10:58:06.720: WARN/System.err(210): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)

12-23 10:58:06.730: WARN/System.err(210): at org.example.android.ValidateUser.onCreate(ValidateUser.java:68)

12-23 10:58:06.730: WARN/System.err(210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)

12-23 10:58:06.730: WARN/System.err(210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2104)

12-23 10:58:06.730: WARN/System.err(210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2157)

12-23 10:58:06.730: WARN/System.err(210): at android.app.ActivityThread.access$1800(ActivityThread.java:112)

12-23 10:58:06.730: WARN/System.err(210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1581)

12-23 10:58:06.730: WARN/System.err(210): at android.os.Handler.dispatchMessage(Handler.java:88)

12-23 10:58:06.730: WARN/System.err(210): at android.os.Looper.loop(Looper.java:123)

12-23 10:58:06.730: WARN/System.err(210): at android.app.ActivityThread.main(ActivityThread.java:3739)

12-23 10:58:06.730: WARN/System.err(210): at java.lang.reflect.Method.invokeNative(Native Method)

12-23 10:58:06.730: WARN/System.err(210): at java.lang.reflect.Method.invoke(Method.java:515)

12-23 10:58:06.730: WARN/System.err(210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)

12-23 10:58:06.730: WARN/System.err(210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)

12-23 10:58:06.730: WARN/System.err(210): at dalvik.system.NativeStart.main(Native Method)


Had the same issue and found this: http://code.google.com/p/ksoap2-android/issues/detail?id=35

See Comments 15 and 17.

They suggest that you wrap the call to "androidHttpTransport.call(SOAP_ACTION, envelope);" in try/catch and try again in case of an error.

OR

Set the system property "http.keepAlive" to "false".

Later on in that issue someone suggests usage of "KeepAliveHttsTransport".

For me, the (I admit it's dirty) try/catch worked just fine.

EDIT typo


Got this working. There were some issues as mentioned above with the Manifest file. On top of that the NAMESPACE, SOAP_ACTION, and URL parameters were incorrect. The NAMESPACE required "uri:" to be pre-pended to it, as did SOAP_ACTION. The URL needed to have an endpoint specified so instead of http://192.168.1.2:8002/WebService.Mobile.svc it needed to be http://192.168.1.2:8002/WebService.Mobile.svc/Mobile. Connection issues resolved, just need to figure out the data transport thing now.

0

精彩评论

暂无评论...
验证码 换一张
取 消