http1.xml
- <TableRow>
- <TextView
- android:text="用户密码:"
- android:id="@+id/TextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- ></TextView>
- <EditText
- android:text=""
- android:id="@+id/pwdEditText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:password="true"></EditText>
- </TableRow>
- <TableRow android:gravity="right">
- <Button
- android:text="取消"
- android:id="@+id/cancelButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- <Button
- android:text="登陆"
- android:id="@+id/loginButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></Button>
- </TableRow>
- </TableLayout>
- /LinearLayout>
四、Web Service编程
TestWebServiceActivity.java
- package com.amaker.ch13.webservice;
- import java.io.IOException;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.MarshalBase64;
- import org.ksoap2.serialization.PropertyInfo;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.AndroidHttpTransport;
- import org.xmlpull.v1.XmlPullParserException;
- import android.app.Activity;
- import android.os.Bundle;
- public class TestWebServiceActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- String serviceNamespace = "http://tempuri.org/";
- String serviceURL = "http://www.ayandy.com/Service.asmx";
- String methodName = "getWeatherbyCityName";
- SoapObject request = new SoapObject(serviceNamespace, methodName);
- PropertyInfo info = new PropertyInfo();
- info.setName("theCityName");
- info.setValue("北京");
- PropertyInfo info2 = new PropertyInfo();
- info2.setName("theDayFlag");
- info2.setValue("1");
- request.addProperty(info);
- request.addProperty(info2);
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
- envelope.bodyOut = request;
- (new MarshalBase64()).register(envelope);
- AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);
- ht.debug = true;
- try {
- ht.call("http://tempuri.org/getWeatherbyCityName", envelope);
- if(envelope.getResponse()!=null){
- System.out.println(envelope.getResult());
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (XmlPullParserException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
WeatherActivity.java
- package com.amaker.ch13.webservice;
- 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.Spinner;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import com.amaker.ch13.R;
- /**
- *
- * 显示天气预报
- */
- public class WeatherActivity extends Activity {
- // 声明视图组件
- private TextView displayTextView;
- private Spinner spinner;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.weather);
- // 实例化视图组件
- displayTextView = (TextView) findViewById(R.id.displayTextView03);
- spinner = (Spinner) findViewById(R.id.citySpinner01);
- List<String> citys = WebServiceUtil.getCityList();
- ArrayAdapter a = new ArrayAdapter(this,
- android.R.layout.simple_spinner_dropdown_item, citys);
- spinner.setAdapter(a);
- spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- String msg = WebServiceUtil.getWeatherMsgByCity(spinner.getSelectedItem().toString());
- displayTextView.setText(msg);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- });
- }
- }
WebServiceUtil.java
- package com.amaker.ch13.webservice;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- 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.protocol.HTTP;
- import org.apache.http.util.EntityUtils;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.MarshalBase64;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.AndroidHttpTransport;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xmlpull.v1.XmlPullParserException;
- /**
- *
- * 天气预报工具类
- */
- public class WebServiceUtil {
- /*
- * 通过传递城市名称获得天气信息
- */
- public static String getWeatherMsgByCity(String cityName) {
- String url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather";
- HttpPost request = new HttpPost(url);
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("theCityCode", cityName));
- params.add(new BasicNameValuePair("theUserID", ""));
- String result = null;
- try {
- UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params,
- HTTP.UTF_8);
- request.setEntity(entity);
- HttpResponse response = new DefaultHttpClient().execute(request);
- if (response.getStatusLine().getStatusCode() == 200) {
- result = EntityUtils.toString(response.getEntity());
- return parse2(result);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /*
- * 使用ksoap,获得城市列表
- */
- public static List<String> getCityList() {
- // 命名空间
- String serviceNamespace = "http://WebXml.com.cn/";
- // 请求URL
- String serviceURL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
- // 调用的方法
- String methodName = "getRegionProvince";
- // 实例化SoapObject对象
- SoapObject request = new SoapObject(serviceNamespace, methodName);
- // 获得序列化的Envelope
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
- envelope.bodyOut = request;
- (new MarshalBase64()).register(envelope);
- // Android传输对象
- AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);
- ht.debug = true;
- try {
- // 调用
- ht.call("http://WebXml.com.cn/getRegionProvince", envelope);
- if (envelope.getResponse() != null) {
- return parse(envelope.bodyIn.toString());
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- }
- return null;
- }
- /*
- * 对天气信息XML文件进行解析
- */
- private static String parse2(String str){
- String temp;
- String[] temps;
- List list = new ArrayList();
- StringBuilder sb = new StringBuilder("");
- if(str!=null&&str.length()>0){
- temp = str.substring(str.indexOf("<string>"));
- temptemps = temp.split("</string>");
- for (int i = 0; i < temps.length; i++) {
- sb.append(temps[i].substring(12));
- sb.append("\n");
- }
- }
- return sb.toString();
- }
- /*
- * 对得到的城市XML信息进行解析
- */
- private static List<String> parse(String str) {
- String temp;
- List<String> list = new ArrayList<String>();
- if (str != null && str.length() > 0) {
- int start = str.indexOf("string");
- int end = str.lastIndexOf(";");
- temp = str.substring(start, end - 3);
- String[] test = temp.split(";");
- for (int i = 0; i < test.length; i++) {
- if (i == 0) {
- temp = test[i].substring(7);
- } else {
- temp = test[i].substring(8);
- }
- int index = temp.indexOf(",");
- list.add(temp.substring(0, index));
- }
- }
- return list;
- }
- }
weather.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:text="天气预报"
- android:id="@+id/titleTextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView
- android:text="请选择城市:"
- android:id="@+id/cityTextView02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- <Spinner
- android:id="@+id/citySpinner01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"></Spinner>
- </LinearLayout>
- <ScrollView
- android:id="@+id/ScrollView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:text="@+id/displayTextView03"
- android:id="@+id/displayTextView03"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"></TextView>
- </ScrollView>
- </LinearLayout>
五、WebView编程
TestWebViewActivity.java
- package com.amaker.ch13.webview;
- import android.app.Activity;
- import android.os.Bundle;
- import android.webkit.WebView;
- import com.amaker.ch13.R;
- /**
- * 通过WebView浏览网络
- */
- public class TestWebViewActivity extends Activity {
- private WebView webView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test_webview);
- webView = (WebView)findViewById(R.id.mywebview);
- /* String url = "http://www.google.com";
- webView.loadUrl(url);*/
- String html = "";
- html+="<html>";
- html+="<body>";
- html+="<a href=http://www.google.com>Google Home</a>";
- html+="</body>";
- html+="</html>";
- webView.loadData(html, "text/html", "utf-8");
- }
- }
test_webview.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <WebView
- android:id="@+id/mywebview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079312