调试通过的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 = "http://WebXml.com.cn/";//不要忘记最后有一个"/"
          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();
          
          }
         
 }
 
 
  
  
 }

你可能感兴趣的:(调试通过的android手机调用网上.net web service的例子)