通过Android使用.NET Web服务

时间:2012-04-10 03:07:51

标签: android web-services ksoap2

我浏览过很多关于使用android消费.net网络服务的文章,但似乎我无法让它发挥作用。

  • 我已经下载“ksoap2-android-assembly-2.6.2-jar-with-dependencies” 并将其作为外部jar添加到我当前的项目中。

  • 我已经把 清单文件中使用权限android:name =“android.permission.INTERNET”

当我调试程序并调用该函数时,应用程序说“应用程序 *(* )已意外停止。请再试一次。”

我做错了什么?这是我的代码:

private static final String NAMESPACE = "http://epson.com.ph/";
private static final String SOAP_ACTION = "http://epson.com.ph/TheMessage";
private static final String METHOD_NAME = "TheMessage";
private static final String URL = "http://10.0.2.2/DataAccess.asmx";

private void getData(){ 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    //request.addProperty("selectStatement", "SELECT * FROM ALLDETAILS");

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

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    TextView tv = (TextView)findViewById(R.id.txtPreview); 

    try { 
        androidHttpTransport.call(SOAP_ACTION, envelope); 
        Object result = (Object)envelope.getResponse();

        tv.setText(result.toString()); 
    } catch (Exception ex) { 
        messageBox(ex.getMessage()); 
        } 
} 

logcat错误是:

04-10 11:04:46.156: E/AndroidRuntime(332): FATAL EXCEPTION: main
04-10 11:04:46.156: E/AndroidRuntime(332): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
04-10 11:04:46.156: E/AndroidRuntime(332):  at my.WSCA.WebServiceConsumptionActivity.getData(WebServiceConsumptionActivity.java:53)
04-10 11:04:46.156: E/AndroidRuntime(332):  at my.WSCA.WebServiceConsumptionActivity.access$0(WebServiceConsumptionActivity.java:52)
04-10 11:04:46.156: E/AndroidRuntime(332):  at my.WSCA.WebServiceConsumptionActivity$1.onClick(WebServiceConsumptionActivity.java:42)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.view.View.performClick(View.java:2485)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.view.View$PerformClick.run(View.java:9080)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.os.Handler.handleCallback(Handler.java:587)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
04-10 11:04:46.156: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 11:04:46.156: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
04-10 11:04:46.156: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
04-10 11:04:46.156: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 11:04:46.156: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 11:04:46.156: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)

请帮助我,我需要尽快完成这项工作。谢谢!

1 个答案:

答案 0 :(得分:0)

感谢您的回复,我已经找到了解决方案。就像弗兰肯斯坦所说的那样,它给出的链接上的情况略有相同。

我在项目中放了 libs 文件夹并导入ksoap jar。我只是想知道,为什么libs文件夹不会自动包含在每个项目中,如果我用不同的文件夹名称命名它,解决方案是否有效。