如何在android中使用soap webservice

时间:2014-10-08 16:41:36

标签: android web-services soap wsdl

我是android的新用户。我想使用soap webservice。我找到了很好的例子,但他们不适合我: - (

这是我的网络服务的网址: http://parsnerkh.com/webservice/server.wsdl

和方法名称是:Cats

输出为:cat1,cat2,...

这是用php测试这个web服务: http://parsnerkh.com/webservice/test-cats.php

请帮帮我。

我使用此代码:

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import com.example.webserviceactivity.R;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private final String NAMESPACE = "urn:AppStore";
    private final String URL = "http://parsnerkh.com/webservice/helloserverwsdl.php";
    private final String SOAP_ACTION = "action:urn:AppStore/AppStorePortType/CatsRequest";
    private final String METHOD_NAME = "Cats";
    private String TAG = "PGGURU";
    private static String celcius;
    private static String fahren;
    Button b;
    TextView tv;
    EditText et;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Celcius Edit Control
        et = (EditText) findViewById(R.id.editText1);
        //Fahrenheit Text control
        tv = (TextView) findViewById(R.id.tv_result);
        //Button to trigger web service invocation
        b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Check if Celcius text control is not empty
                if (et.getText().length() != 0 && et.getText().toString() != "") {
                    //Get the text control value
                    celcius = et.getText().toString();
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute 
                    task.execute();
                //If text control is empty
                } else {
                    tv.setText("Please enter Celcius");
                }
            }
        });
    }

    public void getFahrenheit(String celsius) {
        //Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //Property which holds input parameters
        /*PropertyInfo celsiusPI = new PropertyInfo();
        //Set Name
        celsiusPI.setName("Celsius");
        //Set Value
        celsiusPI.setValue(celsius);
        //Set dataType
        celsiusPI.setType(double.class);
        //Add the property to request object
        request.addProperty(celsiusPI);*/
        //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 {
            //Invole web service
            androidHttpTransport.call(SOAP_ACTION, envelope);
            //Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            //Assign it to fahren static variable
            fahren = response.toString();

        } catch (Exception e) {
            Log.e("ads","ad");
            e.printStackTrace();
        }
    }

    private class AsyncCallWS extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            getFahrenheit(celcius);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
            tv.setText(fahren + "° F");
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
            tv.setText("Calculating...");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

我发现了这个错误:

10-08 22:09:24.536: W/System.err(21468): org.xmlpull.v1.XmlPullParserException: expected: END_TAG {http://schemas.xmlsoap.org/soap/envelope/}Body (position:END_TAG </{http://schemas.xmlsoap.org/soap/envelope/}SOAP-ENV:Fault>@2:209 in java.io.InputStreamReader@418ea258) 
10-08 22:09:24.546: W/System.err(21468):    at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
10-08 22:09:24.546: W/System.err(21468):    at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:138)
10-08 22:09:24.546: W/System.err(21468):    at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
10-08 22:09:24.556: W/System.err(21468):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
10-08 22:09:24.556: W/System.err(21468):    at com.prgguru.android.MainActivity.getFahrenheit(MainActivity.java:85)
10-08 22:09:24.556: W/System.err(21468):    at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:101)
10-08 22:09:24.556: W/System.err(21468):    at com.prgguru.android.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
10-08 22:09:24.556: W/System.err(21468):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-08 22:09:24.556: W/System.err(21468):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-08 22:09:24.556: W/System.err(21468):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-08 22:09:24.556: W/System.err(21468):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-08 22:09:24.556: W/System.err(21468):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-08 22:09:24.556: W/System.err(21468):    at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:0)

使用kso​​ap库来使用android中的soap webservice。 按照下面的教程,它有一个很好的例子来使用Ksoap lib来使用soap服务。 http://code.tutsplus.com/tutorials/consuming-web-services-with-ksoap--mobile-21242