http1.xml

        
  1. <TableRow>
  2. <TextView
  3. android:text="用户密码:"
  4. android:id="@+id/TextView"
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. ></TextView>
  8. <EditText
  9. android:text=""
  10. android:id="@+id/pwdEditText"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:password="true"></EditText>
  14. </TableRow>
  15. <TableRowandroid:gravity="right">
  16. <Button
  17. android:text="取消"
  18. android:id="@+id/cancelButton"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"></Button>
  21. <Button
  22. android:text="登陆"
  23. android:id="@+id/loginButton"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"></Button>
  26. </TableRow>
  27. </TableLayout>
  28. /LinearLayout>

四、Web Service编程

TestWebServiceActivity.java

        
  1. packagecom.amaker.ch13.webservice;
  2. importjava.io.IOException;
  3. importorg.ksoap2.SoapEnvelope;
  4. importorg.ksoap2.serialization.MarshalBase64;
  5. importorg.ksoap2.serialization.PropertyInfo;
  6. importorg.ksoap2.serialization.SoapObject;
  7. importorg.ksoap2.serialization.SoapSerializationEnvelope;
  8. importorg.ksoap2.transport.AndroidHttpTransport;
  9. importorg.xmlpull.v1.XmlPullParserException;
  10. importandroid.app.Activity;
  11. importandroid.os.Bundle;
  12. publicclassTestWebServiceActivityextendsActivity{
  13. @Override
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. StringserviceNamespace="http://tempuri.org/";
  17. StringserviceURL="http://www.ayandy.com/Service.asmx";
  18. StringmethodName="getWeatherbyCityName";
  19. SoapObjectrequest=newSoapObject(serviceNamespace,methodName);
  20. PropertyInfoinfo=newPropertyInfo();
  21. info.setName("theCityName");
  22. info.setValue("北京");
  23. PropertyInfoinfo2=newPropertyInfo();
  24. info2.setName("theDayFlag");
  25. info2.setValue("1");
  26. request.addProperty(info);
  27. request.addProperty(info2);
  28. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
  29. envelope.bodyOut=request;
  30. (newMarshalBase64()).register(envelope);
  31. AndroidHttpTransportht=newAndroidHttpTransport(serviceURL);
  32. ht.debug=true;
  33. try{
  34. ht.call("http://tempuri.org/getWeatherbyCityName",envelope);
  35. if(envelope.getResponse()!=null){
  36. System.out.println(envelope.getResult());
  37. }
  38. }catch(IOExceptione){
  39. //TODOAuto-generatedcatchblock
  40. e.printStackTrace();
  41. }catch(XmlPullParserExceptione){
  42. //TODOAuto-generatedcatchblock
  43. e.printStackTrace();
  44. }
  45. }
  46. }

WeatherActivity.java

        
  1. packagecom.amaker.ch13.webservice;
  2. importjava.util.List;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.widget.AdapterView;
  7. importandroid.widget.ArrayAdapter;
  8. importandroid.widget.Spinner;
  9. importandroid.widget.TextView;
  10. importandroid.widget.AdapterView.OnItemSelectedListener;
  11. importcom.amaker.ch13.R;
  12. /**
  13. *
  14. *显示天气预报
  15. */
  16. publicclassWeatherActivityextendsActivity{
  17. //声明视图组件
  18. privateTextViewdisplayTextView;
  19. privateSpinnerspinner;
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.weather);
  24. //实例化视图组件
  25. displayTextView=(TextView)findViewById(R.id.displayTextView03);
  26. spinner=(Spinner)findViewById(R.id.citySpinner01);
  27. List<String>citys=WebServiceUtil.getCityList();
  28. ArrayAdaptera=newArrayAdapter(this,
  29. android.R.layout.simple_spinner_dropdown_item,citys);
  30. spinner.setAdapter(a);
  31. spinner.setOnItemSelectedListener(newOnItemSelectedListener(){
  32. @Override
  33. publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,
  34. intarg2,longarg3){
  35. Stringmsg=WebServiceUtil.getWeatherMsgByCity(spinner.getSelectedItem().toString());
  36. displayTextView.setText(msg);
  37. }
  38. @Override
  39. publicvoidonNothingSelected(AdapterView<?>arg0){
  40. }
  41. });
  42. }
  43. }

WebServiceUtil.java

        
  1. packagecom.amaker.ch13.webservice;
  2. importjava.io.IOException;
  3. importjava.io.InputStream;
  4. importjava.util.ArrayList;
  5. importjava.util.List;
  6. importjavax.xml.parsers.DocumentBuilder;
  7. importjavax.xml.parsers.DocumentBuilderFactory;
  8. importorg.apache.http.HttpResponse;
  9. importorg.apache.http.NameValuePair;
  10. importorg.apache.http.client.entity.UrlEncodedFormEntity;
  11. importorg.apache.http.client.methods.HttpPost;
  12. importorg.apache.http.impl.client.DefaultHttpClient;
  13. importorg.apache.http.message.BasicNameValuePair;
  14. importorg.apache.http.protocol.HTTP;
  15. importorg.apache.http.util.EntityUtils;
  16. importorg.ksoap2.SoapEnvelope;
  17. importorg.ksoap2.serialization.MarshalBase64;
  18. importorg.ksoap2.serialization.SoapObject;
  19. importorg.ksoap2.serialization.SoapSerializationEnvelope;
  20. importorg.ksoap2.transport.AndroidHttpTransport;
  21. importorg.w3c.dom.Document;
  22. importorg.w3c.dom.Element;
  23. importorg.w3c.dom.Node;
  24. importorg.w3c.dom.NodeList;
  25. importorg.xmlpull.v1.XmlPullParserException;
  26. /**
  27. *
  28. *天气预报工具类
  29. */
  30. publicclassWebServiceUtil{
  31. /*
  32. *通过传递城市名称获得天气信息
  33. */
  34. publicstaticStringgetWeatherMsgByCity(StringcityName){
  35. Stringurl="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather";
  36. HttpPostrequest=newHttpPost(url);
  37. List<NameValuePair>params=newArrayList<NameValuePair>();
  38. params.add(newBasicNameValuePair("theCityCode",cityName));
  39. params.add(newBasicNameValuePair("theUserID",""));
  40. Stringresult=null;
  41. try{
  42. UrlEncodedFormEntityentity=newUrlEncodedFormEntity(params,
  43. HTTP.UTF_8);
  44. request.setEntity(entity);
  45. HttpResponseresponse=newDefaultHttpClient().execute(request);
  46. if(response.getStatusLine().getStatusCode()==200){
  47. result=EntityUtils.toString(response.getEntity());
  48. returnparse2(result);
  49. }
  50. }catch(Exceptione){
  51. e.printStackTrace();
  52. }
  53. returnnull;
  54. }
  55. /*
  56. *使用ksoap,获得城市列表
  57. */
  58. publicstaticList<String>getCityList(){
  59. //命名空间
  60. StringserviceNamespace="http://WebXml.com.cn/";
  61. //请求URL
  62. StringserviceURL="http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
  63. //调用的方法
  64. StringmethodName="getRegionProvince";
  65. //实例化SoapObject对象
  66. SoapObjectrequest=newSoapObject(serviceNamespace,methodName);
  67. //获得序列化的Envelope
  68. SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  69. SoapEnvelope.VER11);
  70. envelope.bodyOut=request;
  71. (newMarshalBase64()).register(envelope);
  72. //Android传输对象
  73. AndroidHttpTransportht=newAndroidHttpTransport(serviceURL);
  74. ht.debug=true;
  75. try{
  76. //调用
  77. ht.call("http://WebXml.com.cn/getRegionProvince",envelope);
  78. if(envelope.getResponse()!=null){
  79. returnparse(envelope.bodyIn.toString());
  80. }
  81. }catch(IOExceptione){
  82. e.printStackTrace();
  83. }catch(XmlPullParserExceptione){
  84. e.printStackTrace();
  85. }
  86. returnnull;
  87. }
  88. /*
  89. *对天气信息XML文件进行解析
  90. */
  91. privatestaticStringparse2(Stringstr){
  92. Stringtemp;
  93. String[]temps;
  94. Listlist=newArrayList();
  95. StringBuildersb=newStringBuilder("");
  96. if(str!=null&&str.length()>0){
  97. temp=str.substring(str.indexOf("<string>"));
  98. temptemps=temp.split("</string>");
  99. for(inti=0;i<temps.length;i++){
  100. sb.append(temps[i].substring(12));
  101. sb.append("\n");
  102. }
  103. }
  104. returnsb.toString();
  105. }
  106. /*
  107. *对得到的城市XML信息进行解析
  108. */
  109. privatestaticList<String>parse(Stringstr){
  110. Stringtemp;
  111. List<String>list=newArrayList<String>();
  112. if(str!=null&&str.length()>0){
  113. intstart=str.indexOf("string");
  114. intend=str.lastIndexOf(";");
  115. temp=str.substring(start,end-3);
  116. String[]test=temp.split(";");
  117. for(inti=0;i<test.length;i++){
  118. if(i==0){
  119. temp=test[i].substring(7);
  120. }else{
  121. temp=test[i].substring(8);
  122. }
  123. intindex=temp.indexOf(",");
  124. list.add(temp.substring(0,index));
  125. }
  126. }
  127. returnlist;
  128. }
  129. }

weather.xml

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:text="天气预报"
  7. android:id="@+id/titleTextView01"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"></TextView>
  10. <LinearLayout
  11. android:orientation="horizontal"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content">
  14. <TextView
  15. android:text="请选择城市:"
  16. android:id="@+id/cityTextView02"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"></TextView>
  19. <Spinner
  20. android:id="@+id/citySpinner01"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"></Spinner>
  23. </LinearLayout>
  24. <ScrollView
  25. android:id="@+id/ScrollView01"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content">
  28. <TextView
  29. android:text="@+id/displayTextView03"
  30. android:id="@+id/displayTextView03"
  31. android:layout_width="fill_parent"
  32. android:layout_height="fill_parent"></TextView>
  33. </ScrollView>
  34. </LinearLayout>

五、WebView编程

TestWebViewActivity.java

        
  1. packagecom.amaker.ch13.webview;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.webkit.WebView;
  5. importcom.amaker.ch13.R;
  6. /**
  7. *通过WebView浏览网络
  8. */
  9. publicclassTestWebViewActivityextendsActivity{
  10. privateWebViewwebView;
  11. @Override
  12. publicvoidonCreate(BundlesavedInstanceState){
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.test_webview);
  15. webView=(WebView)findViewById(R.id.mywebview);
  16. /*Stringurl="http://www.google.com";
  17. webView.loadUrl(url);*/
  18. Stringhtml="";
  19. html+="<html>";
  20. html+="<body>";
  21. html+="<ahref=http://www.google.com>GoogleHome</a>";
  22. html+="</body>";
  23. html+="</html>";
  24. webView.loadData(html,"text/html","utf-8");
  25. }
  26. }

test_webview.xml

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <WebView
  8. android:id="@+id/mywebview"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. />
  12. </LinearLayout>

更多相关文章

  1. android:使用webview加载网页
  2. Android(安卓)中文 API (100) —— ScrollView
  3. android默认字体大小、高度、宽度
  4. android view视图的层叠(叠加)
  5. Android中贪吃蛇游戏的学习(二)
  6. android获得屏幕高度和宽度
  7. Android中文API —— VideoView
  8. Android(安卓)获得屏幕分辨率
  9. Android(安卓)视图动画(View Animation) 使用详解

随机推荐

  1. Android 关于移动互联网寒冬和个人核心竞
  2. Android获取系统储存以及内存信息(二)
  3. Android利用ksoap2写天气预报应用
  4. Android(安卓)实现头像上传功能
  5. Android(安卓)Studio SDK Manager 解决无
  6. android应用性能调试
  7. Android应用资源---本地化(Localization)(一
  8. Android剪贴板详解
  9. 【android】适配多屏幕的最佳实践
  10. Android(安卓)4.1 WebView 页面整体缩放