Android ksoap WebService调用

下载ksoap的jar包,导入到path中就可以了;

 

ksoap 的api:

http://ksoap2.sourceforge.net/doc/api/

 

代码如下;

 

public class WebServiceActivity extends Activity implements OnClickListener {

	private Button btn1, btn2;
	private EditText txt1, txt2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main03);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		txt1 = (EditText) findViewById(R.id.editText1);
		txt2 = (EditText) findViewById(R.id.editText2);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			String nameSpace = "http://WebXml.com.cn/";
			String methodName = "getMobileCodeInfo";
			String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
			String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
			SoapObject rpc = new SoapObject(nameSpace, methodName);
			rpc.addProperty("mobileCode", txt2.getText().toString().trim());
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE transport = new HttpTransportSE(endPoint);
			try {
				transport.call(soapAction, envelope);
			} catch (Exception e) {
				e.printStackTrace();
			}
			SoapObject object = (SoapObject) envelope.bodyIn;
			String result = null;
			if (object == null) {
				result = "没有查询到相关的区域 ";
			} else {
				result = object.getProperty(0).toString();
			}
			txt1.setText(result);
			break;
		case R.id.button2:
			finish();
			break;
		default:
			break;
		}
	}
}

 

 

class MyWeather extends AsyncTask{
		String result = "";
		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String nameSpace = "http://WebXml.com.cn/";
			String methodName = "getWeather";
			String endPoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
			String soapAction = "http://WebXml.com.cn/getWeather";
			SoapObject rpc = new SoapObject(nameSpace, methodName);
			rpc.addProperty("theCityCode", txt2.getText().toString().trim());
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			HttpTransportSE transport = new HttpTransportSE(endPoint);
			try {
				transport.call(soapAction, envelope);
				SoapObject object = (SoapObject) envelope.getResponse();
				if (object == null) {
					result = "没有查询到相关的区域 ";
				} else {
//					for(int a=0;a<object.getPropertyCount();a++){
						result+=object.getProperty(1)+" ;  ";
						result+=object.getProperty(3)+" ;  ";
//					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				result ="exception";
			}
			txt1.post(new Runnable(){
				@Override
				public void run() {
					// TODO Auto-generated method stub
					txt1.setText(result);
				}});
			return null;
		}
		
	}
 

 

 

 

你可能感兴趣的:(webservice)