基本的android Web服务无法正常工作

时间:2014-07-08 11:48:44

标签: android web-services

我只是想用我的android示例项目做一个简单的Web服务,似乎无法让它工作。 这是我的代码:

private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://blah/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private final String METHOD_NAME = "HelloWorld";
private String TAG = "SOAP";
private static String message;

Button b;
TextView tv;


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


    tv = (TextView) findViewById(R.id.textView1);

     b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                    getMessage();

            }
        });
    }

public void getMessage() {

    //Create request
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    //Property which holds input parameters
    PropertyInfo messagePI = new PropertyInfo();
    //Set Name
    messagePI.setName("Practice");
    //Set Value
    messagePI.setValue(messagePI);
    //Set dataType
    messagePI.setType(String.class);
    //Add the property to request object
    request.addProperty(messagePI);
    //Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    //Set output SOAP object
    envelope.setOutputSoapObject(request);
    //Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try {
        //Invoke web service
        androidHttpTransport.call(SOAP_ACTION, envelope);
        //Get the response
        SoapObject result = (SoapObject) envelope.bodyIn;


        if(result !=null){
            tv.setText(result.getProperty(0).toString());
        }


        //tv.setText("" + results);

    } catch (Exception e) {
        tv.setText(e.getMessage());
        //e.printStackTrace();
    }
}

当我单步执行时,它会在SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);行出错,说它来自AndroidRuntime Error ...我真的只是从教程中复制了这段代码,所以它应该可以工作但是可以'似乎弄明白为什么不。

logcat的:

07-08 07:07:20.285: E/AndroidRuntime(6979): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
07-08 07:07:20.285: E/AndroidRuntime(6979):     at com.example.mysoap.MainActivity.getMessage(MainActivity.java:100)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at com.example.mysoap.MainActivity$1.onClick(MainActivity.java:58)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.view.View.performClick(View.java:2482)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.view.View$PerformClick.run(View.java:9077)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.os.Handler.handleCallback(Handler.java:587)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.os.Looper.loop(Looper.java:130)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at java.lang.reflect.Method.invoke(Method.java:507)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-08 07:07:20.285: E/AndroidRuntime(6979):     at dalvik.system.NativeStart.main(Native Method)

以下是我所拥有的图书馆的截图。

enter image description here

这是我现在第二个问题的logcat ......

07-08 09:38:10.515: E/AndroidRuntime(7429): FATAL EXCEPTION: main
07-08 09:38:10.515: E/AndroidRuntime(7429): java.lang.StackOverflowError
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.String._getChars(String.java:1040)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:155)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:215)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219) 
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at   org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serialization.PropertyInfo.toString(PropertyInfo.java:219)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at java.lang.StringBuffer.append(StringBuffer.java:203)
07-08 09:38:10.515: E/AndroidRuntime(7429):     at org.ksoap2.serializatio

这就是它现在错误的行androidHttpTransport.call(SOAP_ACTION, envelope);

1 个答案:

答案 0 :(得分:0)

您需要将ksoap jar文件添加到项目中的libs目录中,以便在构建apk时,jar将被捆绑到其中。您获得的异常告诉我代码未正确捆绑,因此您找不到类错误。