首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Android 调用WebService(讲授一)

2012-09-02 
Android 调用WebService(讲解一)1:导入ksoap2-android 包2:-------------------package gongzibai.co.cci

Android 调用WebService(讲解一)

Android 调用WebService(讲授一)

 

 

1:导入   ksoap2-android 包

2:-------------------
package gongzibai.co.cc;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class WebServiceActivity extends
  Activity {
 Spinner spinner1;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  spinner1 = (Spinner) findViewById(R.id.spinner1);

  //调用webservice 获取省份
  List<String> provinces = WebServiceUtil
    .getProvinceList();

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    this,
    android.R.layout.simple_spinner_item,
    provinces);
  
  adapter.setDropDownViewResource(R.layout.spinner);

  spinner1.setAdapter(adapter);

  spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(
     AdapterView<?> arg0,
     View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub

   }

   @Override
   public void onNothingSelected(
     AdapterView<?> arg0) {
    // TODO Auto-generated method stub

   }
  });
 }
}

----------------WebServiceUtil.java
package gongzibai.co.cc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebServiceUtil {

 static final String SERVICE_NS = "http://WebXml.com.cn/";
 // 定义Web Service提供服务的URL
 static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";

 public static List<String> getProvinceList() {
  // TODO Auto-generated method stub

  String methodName = "getRegionProvince";
  HttpTransportSE ht = new HttpTransportSE(
    SERVICE_URL);
  ht.debug = true;

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);

  SoapObject soapObject = new SoapObject(
    SERVICE_NS, methodName);

  envelope.bodyOut = soapObject;

  envelope.dotNet = true;

  try {
   // 调用Web Service
   ht.call(SERVICE_NS
     + methodName,
     envelope);
   if (envelope.getResponse() != null) {
    // 获取服务器响应返回的SOAP消息
    SoapObject result = (SoapObject) envelope.bodyIn;
    SoapObject detail = (SoapObject) result
      .getProperty(methodName
        + "Result");
    // 解析服务器响应的SOAP消息。
    return parseProvinceOrCity(detail);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } catch (XmlPullParserException e) {
   e.printStackTrace();
  }
  return null;

 }

 private static List<String> parseProvinceOrCity(
   SoapObject detail) {
  // TODO Auto-generated method stub
  ArrayList<String> result = new ArrayList<String>();
  for (int i = 0; i < detail
    .getPropertyCount(); i++) {
   // 解析出每个省份
   result.add(detail
     .getProperty(i)
     .toString()
     .split(",")[0]);
  }
  return result;
 }

}

 

-----------spinner.xml

package gongzibai.co.cc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class WebServiceUtil {

 static final String SERVICE_NS = "http://WebXml.com.cn/";
 // 定义Web Service提供服务的URL
 static final String SERVICE_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";

 public static List<String> getProvinceList() {
  // TODO Auto-generated method stub

  String methodName = "getRegionProvince";
  HttpTransportSE ht = new HttpTransportSE(
    SERVICE_URL);
  ht.debug = true;

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);

  SoapObject soapObject = new SoapObject(
    SERVICE_NS, methodName);

  envelope.bodyOut = soapObject;

  envelope.dotNet = true;

  try {
   // 调用Web Service
   ht.call(SERVICE_NS
     + methodName,
     envelope);
   if (envelope.getResponse() != null) {
    // 获取服务器响应返回的SOAP消息
    SoapObject result = (SoapObject) envelope.bodyIn;
    SoapObject detail = (SoapObject) result
      .getProperty(methodName
        + "Result");
    // 解析服务器响应的SOAP消息。
    return parseProvinceOrCity(detail);
   }
  } catch (IOException e) {
   e.printStackTrace();
  } catch (XmlPullParserException e) {
   e.printStackTrace();
  }
  return null;

 }

 private static List<String> parseProvinceOrCity(
   SoapObject detail) {
  // TODO Auto-generated method stub
  ArrayList<String> result = new ArrayList<String>();
  for (int i = 0; i < detail
    .getPropertyCount(); i++) {
   // 解析出每个省份
   result.add(detail
     .getProperty(i)
     .toString()
     .split(",")[0]);
  }
  return result;
 }

}

 


--------------------------two.方案
package gongzibai.co.cc;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

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

public class WebService1Activity extends
  Activity {
 private String weatherToday;

 private static final String NAMESPACE = "http://WebXml.com.cn/";
 private static final String METHOD_NAME = "getWeatherbyCityName";
 private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
 private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
 private SoapObject detail;
 Button mButton;
 EditText editText1;

 @Override
 protected void onCreate(
   Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  editText1 = (EditText) findViewById(R.id.editText1);
  mButton = (Button) findViewById(R.id.button1);
  mButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    String city = editText1
      .getText()
      .toString();
    getWeather(city);

   }

   private void getWeather(
     String cityName) {
    // TODO Auto-generated method stub
    try {

     SoapObject rpc = new SoapObject(
       NAMESPACE,
       METHOD_NAME);

     rpc.addProperty(
       "theCityName",
       cityName);

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11);
     envelope.bodyOut = rpc;
     envelope.dotNet = true;
     envelope.setOutputSoapObject(rpc);

     HttpTransportSE ht = new HttpTransportSE(
       URL);

     // AndroidHttpTransport ht = new AndroidHttpTransport(URL);
     ht.debug = true;

     ht.call(SOAP_ACTION,
       envelope);
     // ht.call(null, envelope);

     // SoapObject result = (SoapObject)envelope.bodyIn;
     // detail = (SoapObject)
     // result.getProperty("getWeatherbyCityNameResult");

     detail = (SoapObject) envelope
       .getResponse();

     // System.out.println("result" + result);
     System.out
       .println("detail"
         + detail);
     Toast.makeText(
       WebService1Activity.this,
       detail.toString(),
       Toast.LENGTH_LONG)
       .show();
     parseWeather(detail);

     return;
    } catch (Exception e) {
     e.printStackTrace();
    }

   }

   private void parseWeather(
     SoapObject detail) {
    // TODO Auto-generated method stub

    String date = detail
      .getProperty(6)
      .toString();
    weatherToday = "今天:"
      + date.split(" ")[0];
    weatherToday = weatherToday
      + "\n天气:"
      + date.split(" ")[1];
    weatherToday = weatherToday
      + "\n气温:"
      + detail.getProperty(
        5)
        .toString();
    weatherToday = weatherToday
      + "\n风力:"
      + detail.getProperty(
        7)
        .toString()
      + "\n";
    System.out
      .println("weatherToday is "
        + weatherToday);
    Toast.makeText(
      WebService1Activity.this,
      weatherToday,
      Toast.LENGTH_LONG)
      .show();

   }
  });

  SoapObject rpc = new SoapObject(
    NAMESPACE, METHOD_NAME);
  rpc.addProperty("theCityName",
    "北京");

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);
  envelope.bodyOut = rpc;
  envelope.dotNet = true;
  envelope.setOutputSoapObject(rpc);

  HttpTransportSE ht = new HttpTransportSE(
    URL);
  ht.debug = true;
  try {
   ht.call(SOAP_ACTION,
     envelope);
   detail = (SoapObject) envelope
     .getResponse();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (XmlPullParserException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

 

------------------------------------天气调用

package gongzibai.co.cc;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

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

public class WebService1Activity extends
  Activity {
 private String weatherToday;

 private static final String NAMESPACE = "http://WebXml.com.cn/";
 private static final String METHOD_NAME = "wordKey";
 private static String URL = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
 private static String SOAP_ACTION = "http://WebXml.com.cn/wordKey";
 private SoapObject detail;
 Button mButton;
 EditText editText1;
 TextView textView1;

 @Override
 protected void onCreate(
   Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  textView1 = (TextView) findViewById(R.id.textView1);
  editText1 = (EditText) findViewById(R.id.editText1);
  mButton = (Button) findViewById(R.id.button1);
  mButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    String city = editText1
      .getText()
      .toString();
    getWeather(city);

   }

   private void getWeather(
     String cityName) {
    // TODO Auto-generated method stub
    try {

     SoapObject rpc = new SoapObject(
       NAMESPACE,
       METHOD_NAME);

     rpc.addProperty(
       "theCityName",
       cityName);

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
       SoapEnvelope.VER11);
     envelope.bodyOut = rpc;
     envelope.dotNet = true;
     envelope.setOutputSoapObject(rpc);

     HttpTransportSE ht = new HttpTransportSE(
       URL);

     // AndroidHttpTransport ht = new AndroidHttpTransport(URL);
     ht.debug = true;

     ht.call(SOAP_ACTION,
       envelope);
     // ht.call(null, envelope);

     // SoapObject result = (SoapObject)envelope.bodyIn;
     // detail = (SoapObject)
     // result.getProperty("getWeatherbyCityNameResult");

     detail = (SoapObject) envelope
       .getResponse();

     // System.out.println("result" + result);
     textView1
       .setText(detail
         .toString());

     parseWeather(detail);

     return;
    } catch (Exception e) {
     e.printStackTrace();
    }

   }

   private void parseWeather(
     SoapObject detail) {
    // TODO Auto-generated method stub

    String date = detail
      .getProperty(6)
      .toString();
    weatherToday = "今天:"
      + date.split(" ")[0];
    weatherToday = weatherToday
      + "\n天气:"
      + date.split(" ")[1];
    weatherToday = weatherToday
      + "\n气温:"
      + detail.getProperty(
        5)
        .toString();
    weatherToday = weatherToday
      + "\n风力:"
      + detail.getProperty(
        7)
        .toString()
      + "\n";
    System.out
      .println("weatherToday is "
        + weatherToday);
    Toast.makeText(
      WebService1Activity.this,
      weatherToday,
      Toast.LENGTH_LONG)
      .show();

   }
  });

  SoapObject rpc = new SoapObject(
    NAMESPACE, METHOD_NAME);
  rpc.addProperty("theCityName",
    "北京");

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
    SoapEnvelope.VER11);
  envelope.bodyOut = rpc;
  envelope.dotNet = true;
  envelope.setOutputSoapObject(rpc);

  HttpTransportSE ht = new HttpTransportSE(
    URL);
  ht.debug = true;
  try {
   ht.call(SOAP_ACTION,
     envelope);
   detail = (SoapObject) envelope
     .getResponse();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (XmlPullParserException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


 

热点排行