且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android的互联网开发 下

更新时间:2022-10-03 10:20:48

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