Android:使用kso​​ap2通过Android模拟器访问本地主机上的WCF

时间:2013-03-29 20:15:50

标签: android wcf localhost ksoap2

好的,我真的被困在这里!我已经通过很多方式尝试过它,但却有所作为。我试图通过在模拟器上启动的Android应用程序访问wcf web服务(本地)但我的应用程序崩溃,这里是代码。

这是我的Android

      public class MainActivity extends TabActivity {

        /* I am not sure about these !! SOry */ 
 private static final String SOAP_ACTION = "http://localhost:1034/ILocation_Based_Reminder_WCF/getReminder";
 private static final String METHOD_NAME = "getReminder";
 private static final String NAMESPACE = "http://localhost:1034/";
 private static final String URL = "http://localhost:1034/Location_Based_Reminder_WCF.svc/";




@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


private void initialize()
{
    try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

           // request.addProperty("3235466455");

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


            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            try{
            androidHttpTransport.call(SOAP_ACTION, envelope);
            }catch(Exception e){
                System.out.println("this is the message");
                System.out.println(e.getMessage());
                System.out.println("this is stack tracer");
                e.printStackTrace();
            }

            SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

            String resultData = result.toString();

            System.out.println(resultData);
            } catch (Exception e) {
            System.out.println(e.getMessage());
            }
}

这就是网络服务

接口

namespace Location_Based_Reminder
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ILocation_Based_Reminder_WCF" in both code and config file together.
    [ServiceContract]
    public interface ILocation_Based_Reminder_WCF
    {
        [OperationContract]
        String insertUser(long user);
        [OperationContract]
        String insertReminder(long user,String Location,String Time,String Time_Offset,String Date,String Date_Offset,int Status,String Description);
        [OperationContract]
        String getReminder(long userMSISDN);

    }
}

.svc文件

namespace Location_Based_Reminder
{
     public class Location_Based_Reminder_WCF : ILocation_Based_Reminder_WCF
    {
        private BUS _bus;
        private ValueObject _vo;
        private void _initiate()
        {
            _bus = new BUS();
            _vo = new ValueObject();
        }





        #region getReminder
        public String getReminder(long userMSISDN)
        {
            _initiate();
            DataTable _table = new DataTable();
            _table = _bus.getReminder(3235466455);
            String check=null;
            foreach (DataRow row in _table.Rows)
            {
                check = row["reminder_Location"].ToString();
            }
            return check;
        }
        #endregion
    }
}

这是我得到的消息

03-29 20:02:42.245: I/System.out(2102): this is the message
03-29 20:02:42.245: D/AndroidRuntime(2102): Shutting down VM
03-29 20:02:42.245: W/dalvikvm(2102): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-29 20:02:42.255: E/AndroidRuntime(2102): FATAL EXCEPTION: main
03-29 20:02:42.255: E/AndroidRuntime(2102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.locationbasedreminderversionOne.Two/com.example.locationbasedreminderversionOne.Two.MainActivity}: java.lang.NullPointerException
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.os.Looper.loop(Looper.java:137)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at dalvik.system.NativeStart.main(Native Method)
03-29 20:02:42.255: E/AndroidRuntime(2102): Caused by: java.lang.NullPointerException
03-29 20:02:42.255: E/AndroidRuntime(2102):     at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at com.example.locationbasedreminderversionOne.Two.MainActivity.initialize(MainActivity.java:116)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at com.example.locationbasedreminderversionOne.Two.MainActivity.onCreate(MainActivity.java:40)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.Activity.performCreate(Activity.java:5104)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-29 20:02:42.255: E/AndroidRuntime(2102):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-29 20:02:42.255: E/AndroidRuntime(2102):     ... 11 more

我在android清单中添加了permisions和stuff。请帮忙。提前致谢

3 个答案:

答案 0 :(得分:0)

这里的问题是您需要从您设计的服务中获取SOAP。示例代码如下。

       String SOAP_ACTION = "http://tempuri.org/WriteToDB";
       String NAMESPACE = "http://tempuri.org/";
       String METHOD_NAME = "WriteToDB";
       String URL = "http://10.224.185.37/AndroidService.asmx?op=WriteToDB";

答案 1 :(得分:0)

好吧我弄清楚了问题,只要android中涉及网络通信,就不应该直接在onCreate方法中完成,因为应该使用异步活动。

答案 2 :(得分:0)

ondoir代码上的“localhost”是指android的localhost。 10.0.2.2是计算机本地主机的虚拟IP; - )