Android调用本地WebService

package com.example.testinvokewebservice;



import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;



import android.content.Context;

import android.os.AsyncTask;

import android.util.Log;

import android.widget.TextView;

import android.widget.Toast;



public class AsyncHttpTask extends AsyncTask<Integer, Integer, String> {



    private String strRes="";

    TextView textView;

    public Context context;

    @Override

    protected String doInBackground(Integer... params) {

        // TODO Auto-generated method stub

        try

        {

            // WS的命名空间

            final String targetNameSpace = "http://tempuri.org/";  

            // WSDL文档中的URL  

            final String WSDL = "http://192.168.1.25/HH.asmx";  

            //要调用的方法名

            final String methodName = "HelloWorld";  

            //查看WebService会得到此对应的SoapAction

            String soapAction = "http://tempuri.org/HelloWorld";

            SoapObject request = new SoapObject(targetNameSpace, methodName);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.bodyOut = request;

//            envelope.dotNet = true;

            HttpTransportSE http = new HttpTransportSE(WSDL);

//            http.debug = true;

            //网上好多扯蛋的,说第一个参数为NULL可以,实测不能为NULL

            http.call(soapAction, envelope);

            if(envelope.getResponse() != null)

            {

//                SoapObject result = (SoapObject)envelope.getResponse();

                strRes = envelope.getResponse().toString();

                return strRes;

            }

        }

        catch(Exception ex)

        {

            ex.printStackTrace();

        }

        return strRes;

    }



    /* (non-Javadoc)

     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)

     */

    @Override

    protected void onPostExecute(String result) {

        // TODO Auto-generated method stub

        Toast to = Toast.makeText(context, strRes, Toast.LENGTH_LONG);

        to.show();

    }



    

}



class Para{

    

    

}
package com.example.testinvokewebservice;



import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;



public class MainActivity extends Activity {

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        Button btn= (Button)findViewById(R.id.btnLogin);

        btn.setOnClickListener(new View.OnClickListener() {

            

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                AsyncHttpTask http = new AsyncHttpTask();

                http.context = getApplicationContext();

                http.execute();

            }

        });

    }

}

 AndroidManifest.xml增加权限方面的配置:

    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" >

    </uses-permission>

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >

    </uses-permission>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >

    </uses-permission>

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >

    </uses-permission>

 

你可能感兴趣的:(webservice)