码迷,mamicode.com
首页 > 移动开发 > 详细

Android调用本地WebService

时间:2015-06-12 16:53:20      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

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();
            }
        });
    }
}

 

Android调用本地WebService

标签:

原文地址:http://www.cnblogs.com/niuge/p/4571804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!