android的互联网开发 下

简介:

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.              
  9.            <EditText   
  10.                android:text=""   
  11.                android:id="@+id/pwdEditText" 
  12.                android:layout_width="fill_parent"   
  13.                android:layout_height="wrap_content" 
  14.                android:password="true"></EditText> 
  15.        </TableRow> 
  16.          
  17.        <TableRow android:gravity="right"> 
  18.            <Button   
  19.                android:text="取消"   
  20.                android:id="@+id/cancelButton" 
  21.                android:layout_width="wrap_content"   
  22.                android:layout_height="wrap_content"></Button> 
  23.      
  24.            <Button   
  25.                android:text="登陆"   
  26.                android:id="@+id/loginButton" 
  27.                android:layout_width="wrap_content"   
  28.                android:layout_height="wrap_content"></Button> 
  29.        </TableRow> 
  30.  
  31.    </TableLayout> 
  32.  
  33. /LinearLayout> 

四、Web Service编程

TestWebServiceActivity.java

 

 
  1. package com.amaker.ch13.webservice;  
  2.  
  3. import java.io.IOException;  
  4.  
  5. import org.ksoap2.SoapEnvelope;  
  6. import org.ksoap2.serialization.MarshalBase64;  
  7. import org.ksoap2.serialization.PropertyInfo;  
  8. import org.ksoap2.serialization.SoapObject;  
  9. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  10. import org.ksoap2.transport.AndroidHttpTransport;  
  11. import org.xmlpull.v1.XmlPullParserException;  
  12.  
  13. import android.app.Activity;  
  14. import android.os.Bundle;  
  15.  
  16. public class TestWebServiceActivity extends Activity {  
  17.       
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         String serviceNamespace = "http://tempuri.org/";  
  22.         String serviceURL = "http://www.ayandy.com/Service.asmx";  
  23.         String methodName = "getWeatherbyCityName";    
  24.             
  25.         SoapObject request = new SoapObject(serviceNamespace, methodName);  
  26.           
  27.         PropertyInfo info = new PropertyInfo();  
  28.         info.setName("theCityName");  
  29.         info.setValue("北京");  
  30.           
  31.         PropertyInfo info2 = new PropertyInfo();  
  32.         info2.setName("theDayFlag");  
  33.         info2.setValue("1");  
  34.           
  35.         request.addProperty(info);  
  36.         request.addProperty(info2);  
  37.           
  38.         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  39.         envelope.bodyOut = request;  
  40.         (new MarshalBase64()).register(envelope);  
  41.           
  42.         AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL);  
  43.           
  44.         ht.debug = true;  
  45.           
  46.         try {  
  47.             ht.call("http://tempuri.org/getWeatherbyCityName", envelope);  
  48.             if(envelope.getResponse()!=null){  
  49.                 System.out.println(envelope.getResult());  
  50.             }  
  51.         } catch (IOException e) {  
  52.             // TODO Auto-generated catch block  
  53.             e.printStackTrace();  
  54.         } catch (XmlPullParserException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         }  
  58.  
  59.           
  60.     }  

WeatherActivity.java

 

 
  1. package com.amaker.ch13.webservice;  
  2.  
  3. import java.util.List;  
  4.  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Spinner;  
  11. import android.widget.TextView;  
  12. import android.widget.AdapterView.OnItemSelectedListener;  
  13. import com.amaker.ch13.R;  
  14.  
  15. /**  
  16.  *   
  17.  * 显示天气预报  
  18.  */  
  19. public class WeatherActivity extends Activity {  
  20.     // 声明视图组件  
  21.     private TextView displayTextView;  
  22.     private Spinner spinner;  
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.  
  27.         setContentView(R.layout.weather);  
  28.         // 实例化视图组件  
  29.         displayTextView = (TextView) findViewById(R.id.displayTextView03);  
  30.         spinner = (Spinner) findViewById(R.id.citySpinner01);  
  31.           
  32.         List<String> citys = WebServiceUtil.getCityList();  
  33.         ArrayAdapter a = new ArrayAdapter(this,  
  34.                 android.R.layout.simple_spinner_dropdown_item, citys);  
  35.         spinner.setAdapter(a);  
  36.  
  37.         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
  38.  
  39.             @Override  
  40.             public void onItemSelected(AdapterView<?> arg0, View arg1,  
  41.                     int arg2, long arg3) {  
  42.                 String msg = WebServiceUtil.getWeatherMsgByCity(spinner.getSelectedItem().toString());  
  43.                 displayTextView.setText(msg);  
  44.             }  
  45.             @Override  
  46.             public void onNothingSelected(AdapterView<?> arg0) {  
  47.                   
  48.             }  
  49.         });  
  50.           
  51.  
  52.     }  

WebServiceUtil.java

 

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

weather.xml

 

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

五、WebView编程

TestWebViewActivity.java

 

 
  1. package com.amaker.ch13.webview;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.webkit.WebView;  
  6.  
  7. import com.amaker.ch13.R;  
  8. /**  
  9.  * 通过WebView浏览网络  
  10.  */  
  11. public class TestWebViewActivity extends Activity {  
  12.     private WebView webView;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.test_webview);  
  17.         webView = (WebView)findViewById(R.id.mywebview);  
  18.        /* String url = "http://www.google.com";  
  19.         webView.loadUrl(url);*/  
  20.           
  21.         String html = "";  
  22.         html+="<html>";  
  23.             html+="<body>";  
  24.                 html+="<a href=http://www.google.com>Google Home</a>";  
  25.             html+="</body>";  
  26.         html+="</html>";  
  27.           
  28.         webView.loadData(html, "text/html", "utf-8");  
  29.           
  30.           
  31.     }  

test_webview.xml

 

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

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079312



相关文章
|
6天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
19天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
3天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
18 7
|
6天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
3天前
|
缓存 Java Linux
探索安卓开发:从新手到专家的旅程
【9月更文挑战第30天】在这篇文章中,我们将一起踏上一段激动人心的旅程,探索安卓开发的广阔世界。无论你是刚入门的新手,还是希望提升技能的开发者,本文都将为你提供宝贵的知识和指导。我们将深入探讨安卓开发的基础知识、关键概念、实用工具和最佳实践,帮助你在安卓开发领域取得更大的成功。让我们一起开启这段精彩的旅程吧!
|
4天前
|
监控 安全 Java
Kotlin 在公司上网监控中的安卓开发应用
在数字化办公环境中,公司对员工上网行为的监控日益重要。Kotlin 作为一种基于 JVM 的编程语言,具备简洁、安全、高效的特性,已成为安卓开发的首选语言之一。通过网络请求拦截,Kotlin 可实现网址监控、访问时间记录等功能,满足公司上网监控需求。其简洁性有助于快速构建强大的监控应用,并便于后续维护与扩展。因此,Kotlin 在安卓上网监控应用开发中展现出广阔前景。
7 1
|
14天前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
38 10
|
7天前
|
存储 开发工具 Android开发
使用.NET MAUI开发第一个安卓APP
【9月更文挑战第24天】使用.NET MAUI开发首个安卓APP需完成以下步骤:首先,安装Visual Studio 2022并勾选“.NET Multi-platform App UI development”工作负载;接着,安装Android SDK。然后,创建新项目时选择“.NET Multi-platform App (MAUI)”模板,并仅针对Android平台进行配置。了解项目结构,包括`.csproj`配置文件、`Properties`配置文件夹、平台特定代码及共享代码等。
|
16天前
|
Java Linux Android开发
深入理解Android开发:从基础到高级
【9月更文挑战第17天】本文将深入探讨Android开发的各个方面,包括应用开发、操作系统等。我们将通过代码示例来展示如何创建一个简单的Android应用,并解释其背后的原理。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和启示。
|
13天前
|
存储 Java Android开发
🔥Android开发大神揭秘:从菜鸟到高手,你的代码为何总是慢人一步?💻
在Android开发中,每位开发者都渴望应用响应迅速、体验流畅。然而,代码执行缓慢却是常见问题。本文将跟随一位大神的脚步,剖析三大典型案例:主线程阻塞导致卡顿、内存泄漏引发性能下降及不合理布局引起的渲染问题,并提供优化方案。通过学习这些技巧,你将能够显著提升应用性能,从新手蜕变为高手。
16 2
下一篇
无影云桌面