基于ksoap2实现火车时刻查询

Home / Android MrLee 2015-5-14 3404

WebService 是一种基于SOAP协议的远程调用标准。通过WebService可以将不同操作系统平台,不同语言、不同技术整合到一起。在Android SDK中并没有提供调用WebService的库,因此,需要使用第三方类库(KSOAP2)来调用WebService。在本文将介绍在Android 中调用WebService的具体细节,并在最后给出一个完整的例子来演示如何使用KSOAP2来调用WebService。 安装第三方类库:KSOAP2 PC版本的WebService客户端类库非常丰富,例如,Axis2、CXF等,但这些类库对于Android系统过于庞大,也未必很容易移植到 Android系统上。因此,这些开发包并不在我们考虑的范围内。适合手机的WebService客户端类库也有一些。本例使用了比较常用的 KSOAP2。读者可以从如下的地址下载Android版的KSOAP2。 将下载后的jar文件复制到Eclipse工程的lib目录中(如果没有该目录,可以新建一个,当然,也可以放在其他的目录中)。并在Eclipse工程中引用这个jar包
部分源码:
package com.wxx.TrainTime;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.wxx.CheCi.Tool;
import com.wxx.CheCi.checi2;
public class MainActivity extends Activity implements OnClickListener {
	EditText train_editText1, train_editText2;
	Button train_button1, train_checi, train_chaxun;
	TextView train_textView1, train_textView2;
	private String one;
	private String two;
	boolean fg = false;
	int i = 0;
	Tool to;
	ProgressDialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		inti();
	}
	private void inti() {
		train_textView2 = (TextView) findViewById(R.id.train_textView2);
		train_textView1 = (TextView) findViewById(R.id.train_textView1);
		// 查询
		train_button1 = (Button) findViewById(R.id.train_button1);
		train_chaxun = (Button) findViewById(R.id.train_chaxun);
		// 车次查询
		train_checi = (Button) findViewById(R.id.train_checi);
		// 始发站
		train_editText1 = (EditText) findViewById(R.id.train_editText1);
		// 抵达站
		train_editText2 = (EditText) findViewById(R.id.train_editText2);
		train_button1.setOnClickListener(this);
		train_checi.setOnClickListener(this);
		train_chaxun.setOnClickListener(this);
		/*
		 * String hao=getIntent().getStringExtra("haoma"); if(hao.equals("1")){
		 * train_textView2.setVisibility(View.VISIBLE);
		 * train_editText2.setVisibility(View.VISIBLE); }
		 */
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 查询
		case R.id.train_button1:
			one = train_editText1.getText().toString().trim();
			two = train_editText2.getText().toString().trim();
			Intent intent = new Intent(MainActivity.this, Train_item.class);
			intent.putExtra("one", one);
			intent.putExtra("two", two);
			startActivity(intent);
			MainActivity.this.finish();
			break;
		// 车次查询
		case R.id.train_checi:
			Intent in = new Intent(MainActivity.this, checi2.class);
			in.putExtra("haoma", "2");
			startActivity(in);
			break;
		case R.id.train_chaxun:
			train_textView1.setText("始发站:");
			train_textView2.setVisibility(View.VISIBLE);
			train_editText2.setVisibility(View.VISIBLE);
			train_editText1.setHint("请输入始发站");
			break;
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 捕捉返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(MainActivity.this)
					.setTitle("提示")
					.setMessage("确定要退出吗?")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									MainActivity.this.finish();
								}
							}).setNegativeButton("取消", null).show();
		}
		return super.onKeyDown(keyCode, event);
	}
	/*
	 * // 采用soap协议 class SoapTask extends AsyncTask {
	 * 
	 * @Override protected String doInBackground(String... params) { String s =
	 * null; to = new Tool(); // 指明要连接的地址 try { HttpTransportSE hs = new
	 * HttpTransportSE(
	 * "http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx");
	 * SoapSerializationEnvelope arg1 = new SoapSerializationEnvelope(
	 * SoapSerializationEnvelope.VER11); arg1.dotNet = true;
	 * 
	 * // 设置要提交的封装数据 SoapObject soapObject = new
	 * SoapObject("http://WebXml.com.cn/", "getStationAndTimeByTrainCode");
	 * soapObject.addProperty("TrainCode", params[0]);
	 * soapObject.addProperty("UserID", "");
	 * arg1.setOutputSoapObject(soapObject);
	 * 
	 * hs.call("http://WebXml.com.cn/getStationAndTimeByTrainCode", arg1);
	 * SoapObject response = (SoapObject) arg1.bodyIn; s =
	 * response.getProperty(0).toString();
	 * 
	 * // 获取每一个返回的值 SoapObject so = (SoapObject) arg1.getResponse();
	 * to.setTrainCode(so.getProperty(0).toString());
	 * to.setFirstStation(so.getProperty(1).toString());
	 * to.setLastStation(so.getProperty(2).toString());
	 * to.setStartTime(so.getProperty(4).toString());
	 * to.setArriveTime(so.getProperty(6).toString());
	 * to.setUseDate(so.getProperty(8).toString()); } catch (IOException e) { //
	 * TODO Auto-generated catch block e.printStackTrace(); } catch
	 * (XmlPullParserException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); } return s; }
	 * 
	 * @Override protected void onPostExecute(String result) { String TrainCode
	 * = to.getTrainCode(); String FirstStation = to.getFirstStation(); String
	 * LastStation = to.getLastStation(); String StartTime = to.getStartTime();
	 * String ArriveTime = to.getArriveTime(); String UseDate = to.getUseDate();
	 * Intent in = new Intent(MainActivity.this, Checi.class);
	 * in.putExtra("TrainCode", TrainCode); in.putExtra("FirstStation",
	 * FirstStation); in.putExtra("LastStation", LastStation);
	 * in.putExtra("StartTime", StartTime); in.putExtra("ArriveTime",
	 * ArriveTime); in.putExtra("UseDate", UseDate); startActivity(in); } }
	 */
}
package com.wxx.TrainTime;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class MyCofig {
	private static String result, jieguo;
	private static InputStream result1;
	public static InputStream luxian(String StartStation, String ArriveStation,
			String UserID) {
		HttpClient hc = new DefaultHttpClient();
		HttpPost hp = new HttpPost(
				"http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByStationName");
		ArrayList list = new ArrayList();
		list.add(new BasicNameValuePair("StartStation", StartStation));
		list.add(new BasicNameValuePair("ArriveStation", ArriveStation));
		list.add(new BasicNameValuePair("UserID", UserID));
		try {
			hp.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse hr = hc.execute(hp);
			if (hr.getStatusLine().getStatusCode() == 200) {
				HttpEntity he = hr.getEntity();
				result = EntityUtils.toString(he);
				result1 = new ByteArrayInputStream(result.getBytes("utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result1;
	}
	public static InputStream mingxi(String TrainCode, String UserID) {
		HttpClient hc = new DefaultHttpClient();
		HttpPost hp = new HttpPost(
				"http://webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode");
		ArrayList list = new ArrayList();
		list.add(new BasicNameValuePair("TrainCode", TrainCode));
		list.add(new BasicNameValuePair("UserID", UserID));
		try {
			hp.setEntity(new UrlEncodedFormEntity(list, "utf-8"));
			HttpResponse hr = hc.execute(hp);
			if (hr.getStatusLine().getStatusCode() == 200) {
				HttpEntity he = hr.getEntity();
				jieguo = EntityUtils.toString(he);
				result1 = new ByteArrayInputStream(jieguo.getBytes("utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result1;
	}
}
工程项目代码一般般,仅供学习参考!
工程源码:火车时刻表

本文链接:https://www.it72.com/2881.htm

推荐阅读
最新回复 (0)
返回