使用KSOAP2和Android在Azure上访问WCF Web服务

时间:2012-03-07 00:57:33

标签: android wcf azure ksoap2

我是Windows Azure和WCF的新手,我正在尝试使用kso​​ap2库访问从Android部署到Windows Azure的非常简单的Web服务。

private static final String SOAP_ACTION = "http://tempuri.org/IService1/GetMyNumber";
private static final String METHOD_NAME = "GetMyNumber";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://6d027531c7444d098932c25e238597f3.cloudapp.net/Service1.svc";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.text1);

   try {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //String hello = "";

        request.addProperty("tuba", "7");

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

        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);

        //Object result = (Object)envelope.getResponse();

        //Integer result = (Integer)  result;
        //tv.setText( ""+result);
    }
    catch (Exception e) {
        tv.setText(e.getMessage());
    }
}

另一方面,我的wcf网络服务看起来像这样

namespace WCFServiceWebRole1
{

    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetHello();

        [WebInvoke]
        int GetMyNumber(int tuba);

    }

}

通过该实施:

namespace WCFServiceWebRole1
{

    public class Service1 : IService1
    {
        public string GetHello()
        {
            return "Hello from my WCF service in Windows Azure!";
        }

        public int GetMyNumber(int tuba) {
            return tuba + 7;
        }

    }
}

我正在使用Galaxy Tab 10.1。然后它给出一个错误,导致平板电脑强行关闭。 问题是由这一行引起的:

androidHttpTransport.call(SOAP_ACTION, envelope);

来自logcat i的错误日志

03-07 21:50:14.090: E/AndroidRuntime(1085): FATAL EXCEPTION: main
03-07 21:50:14.090: E/AndroidRuntime(1085): java.lang.RuntimeException: Unable to start activity ComponentInfo{crm.target.application/crm.target.application.ProductActivity}: java.lang.NullPointerException
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1592)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:130)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:342)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:676)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.widget.TabHost.setCurrentTab(TabHost.java:345)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:149)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:559)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.view.View.performClick(View.java:3122)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.view.View$PerformClick.run(View.java:11942)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.os.Handler.handleCallback(Handler.java:587)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.os.Looper.loop(Looper.java:132)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.ActivityThread.main(ActivityThread.java:4028)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at java.lang.reflect.Method.invoke(Method.java:491)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at dalvik.system.NativeStart.main(Native Method)
03-07 21:50:14.090: E/AndroidRuntime(1085): Caused by: java.lang.NullPointerException
03-07 21:50:14.090: E/AndroidRuntime(1085):     at crm.target.application.ProductActivity.onCreate(ProductActivity.java:50)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
03-07 21:50:14.090: E/AndroidRuntime(1085):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)

它连接到互联网,我允许在我的Android应用程序上使用互联网。此外,我使用C#控制台应用程序测试了我的WCF Web服务,它可以工作。

我该如何解决这个问题?

0 个答案:

没有答案