调试通过的android手机调用网上.net web service的例子

 

搭建了android环境,至少有2种方法实现调用webservice

1 用java直接socket编程

2调用封装好的ksoap2-android-assembly-2.4-jar-with-dependencies.jar
用第2中方法实现了,步骤
1 下载ksoap2-android-assembly-2.4-jar-with-dependencies.jar, 并导入工程
     右键工程 -> build path -〉add libraries
      ->user library -> user libraries -> new
      起一个名字然后 add jars 导入ksoap2的jar
2 在AndroidManifest.xml的清单中加入<uses-permission android:name="android.permission.INTERNET" />

下面是我调试通过的android手机调用网上web service的例子

 

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 
 TextView txt;
 EditText edit1;
 EditText edit2;
 Button btn;
   
 @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       txt = (TextView)findViewById(R.id.txt);
       edit1 = (EditText)findViewById(R.id.edit1);
       edit2= (EditText)findViewById(R.id.edit2);
       btn= (Button)findViewById(R.id.btn);
      
 

          String nameSpace = "不要忘记最后有一个"/"
          String serviceURL = "
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL";
          String methodName = "getWeatherbyCityName";
          String soapAction = "http://WebXml.com.cn/getWeatherbyCityName";//namespace+method
         

          SoapObject request = new SoapObject(nameSpace, methodName);
          request.addProperty("theCityName", "BeiJing");
//          request.addProperty("CountryName","China");
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
          envelope.bodyOut = request;
          envelope.dotNet=true;
          HttpTransportSE ht = new HttpTransportSE(serviceURL);
          ht.debug = true;
          try {
              //ht.call(soapAction, envelope);
           //null?需更改?
           ht.call(soapAction, envelope);
          
         // 获取返回的数据

           SoapObject object = (SoapObject) envelope.bodyIn;

           // 获取返回的结果

//           String result = object.getProperty("Time").toString();
//           Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();

         
             if (envelope.getResponse() != null) {
              String str= envelope.getResponse().toString();
                 Toast.makeText(getApplication(), str, Toast.LENGTH_LONG).show();
             }    
            else
              {
            
              Toast.makeText(getApplication(), "未得到值", 1).show();
              
              }
            }
          catch(Exception ex)
          {
           Toast.makeText(getApplication(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
          
          }
         
 }
 
 
  
  
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值