0

好吧,我真的被困在这里了!我尝试了很多方法,但都是徒劳的。我正在尝试通过在模拟器上启动的 android 应用程序访问 wcf Web 服务(本地),但我的应用程序崩溃了,这里是代码。

这是我的安卓

      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 manifest 中添加了权限和东西。请帮忙。提前致谢

4

3 回答 3

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";
于 2013-03-31T14:46:57.923 回答
0

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

于 2013-04-10T20:14:46.600 回答
0

好吧,我发现了这个问题,每当android中涉及网络通信时,都不应该直接在onCreate方法中完成,因为应该使用这个异步活动。

于 2013-04-03T16:37:24.273 回答