編輯:Android開發實例
相信大家在平常的開發中,對網絡的操作用到HTTP協議比較多,通過我們使用Get或者Post的方法調用一個數據接口,然後服務器給我們返回JSON格式的數據,我們解析JSON數據然後展現給用戶,相信很多人很喜歡服務器給我們返回JSON數據格式,因為他解析方便,也有一些JSON的解析庫,例如Google提供的GSON,阿裡巴巴的FastJson,不過還是推薦大家使用FastJson來解析,我自己開發中也是用FastJson來解析,FastJson的介紹http://code.alibabatech.com/wiki/display/FastJSON/Home ,不過有時候我們用到WebService接口來獲取數據, WebService是一種基於SOAP協議的遠程調用標准,通過webservice可以將不同操作系統平台、不同語言、不同技術整合到一塊。在Android SDK中並沒有提供調用WebService的庫,因此,需要使用第三方的SDK來調用WebService。PC版本的WEbservice客戶端庫非常豐富,例如Axis2,CXF等,但這些開發包對於Android系統過於龐大,也未必很容易移植到Android系統中。因此,這些開發包並不是在我們的考慮范圍內。適合手機的WebService客戶端的SDK有一些,比較常用的有Ksoap2,可以從http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2 進行下載,將jar包加入到libs目錄下就行了,接下來帶大家來調用WebService接口
首先我們新建一個工程,取名WebServiceDemo,我們從http://www.webxml.com.cn/zh_cn/web_services.aspx 來獲取WebService接口,這裡面有一些免費的WebService接口,我們就用裡面的天氣接口吧http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
我們新建一個WebService的工具類,用於對WebService接口的調用,以後遇到調用WebService直接拷貝來用就行了
- package com.example.webservicedemo;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpResponseException;
- import org.ksoap2.transport.HttpTransportSE;
- import org.xmlpull.v1.XmlPullParserException;
- import android.os.Handler;
- import android.os.Message;
- /**
- * 訪問WebService的工具類,
- *
- *
- * @author xiaanming
- *
- */
- public class WebServiceUtils {
- public static final String WEB_SERVER_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
- // 含有3個線程的線程池
- private static final ExecutorService executorService = Executors
- .newFixedThreadPool(3);
- // 命名空間
- private static final String NAMESPACE = "http://WebXml.com.cn/";
- /**
- *
- * @param url
- * WebService服務器地址
- * @param methodName
- * WebService的調用方法名
- * @param properties
- * WebService的參數
- * @param webServiceCallBack
- * 回調接口
- */
- public static void callWebService(String url, final String methodName,
- HashMap<String, String> properties,
- final WebServiceCallBack webServiceCallBack) {
- // 創建HttpTransportSE對象,傳遞WebService服務器地址
- final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
- // 創建SoapObject對象
- SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
- // SoapObject添加參數
- if (properties != null) {
- for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
- .iterator(); it.hasNext();) {
- Map.Entry<String, String> entry = it.next();
- soapObject.addProperty(entry.getKey(), entry.getValue());
- }
- }
- // 實例化SoapSerializationEnvelope,傳入WebService的SOAP協議的版本號
- final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
- // 設置是否調用的是.Net開發的WebService
- soapEnvelope.setOutputSoapObject(soapObject);
- soapEnvelope.dotNet = true;
- httpTransportSE.debug = true;
- // 用於子線程與主線程通信的Handler
- final Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- // 將返回值回調到callBack的參數中
- webServiceCallBack.callBack((SoapObject) msg.obj);
- }
- };
- // 開啟線程去訪問WebService
- executorService.submit(new Runnable() {
- @Override
- public void run() {
- SoapObject resultSoapObject = null;
- try {
- httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
- if (soapEnvelope.getResponse() != null) {
- // 獲取服務器響應返回的SoapObject
- resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
- }
- } catch (HttpResponseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- } finally {
- // 將獲取的消息利用Handler發送到主線程
- mHandler.sendMessage(mHandler.obtainMessage(0,
- resultSoapObject));
- }
- }
- });
- }
- /**
- *
- *
- * @author xiaanming
- *
- */
- public interface WebServiceCallBack {
- public void callBack(SoapObject result);
- }
- }
我們通過調用裡面的callWebService(String url, final String methodName,HashMap<String, String> properties,final WebServiceCallBack webServiceCallBack)就可以來獲取我們想要的數據,現在講解下裡面的實現思路
我們先用一個ListView來顯示所有的省份,然後點擊每個省進去到市。市也用一個ListView來顯示,最後點擊市用TextView來顯示獲取的WebService天氣情況,思路很簡單
用來顯示省份的布局,裡面只有一個ListView
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <ListView
- android:id="@+id/province_list"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:cacheColorHint="@android:color/transparent"
- android:fadingEdge="none" >
- </ListView>
- </RelativeLayout>
接下來就是Activity的代碼,先用工具類調用WebService方法,然後在回調方法callBack(SoapObject result)中解析數據到一個List<String>中,在設置ListView的適配器
- package com.example.webservicedemo;
- import java.util.ArrayList;
- import java.util.List;
- import org.ksoap2.serialization.SoapObject;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
- /**
- * 顯示天氣省份的Activity
- *
- *
- * @author xiaanming
- *
- */
- public class MainActivity extends Activity {
- private List<String> provinceList = new ArrayList<String>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- final ListView mProvinceList = (ListView) findViewById(R.id.province_list);
- //顯示進度條
- ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
- //通過工具類調用WebService接口
- WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportProvince", null, new WebServiceCallBack() {
- //WebService接口返回的數據回調到這個方法中
- @Override
- public void callBack(SoapObject result) {
- //關閉進度條
- ProgressDialogUtils.dismissProgressDialog();
- if(result != null){
- provinceList = parseSoapObject(result);
- mProvinceList.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provinceList));
- }else{
- Toast.makeText(MainActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
- }
- }
- });
- mProvinceList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- Intent intent = new Intent(MainActivity.this, CityActivity.class);
- intent.putExtra("province", provinceList.get(position));
- startActivity(intent);
- }
- });
- }
- /**
- * 解析SoapObject對象
- * @param result
- * @return
- */
- private List<String> parseSoapObject(SoapObject result){
- List<String> list = new ArrayList<String>();
- SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportProvinceResult");
- if(provinceSoapObject == null) {
- return null;
- }
- for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
- list.add(provinceSoapObject.getProperty(i).toString());
- }
- return list;
- }
- }
點擊省份進入該省份下面的市。也用一個ListView來顯示市的數據,布局跟上面一樣,Activity裡面的代碼也差不多相似,我就不過多說明了,直接看代碼
- package com.example.webservicedemo;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.ksoap2.serialization.SoapObject;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
- /**
- * 顯示城市的Activity
- *
- *
- * @author xiaanming
- *
- */
- public class CityActivity extends Activity {
- private List<String> cityStringList;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- final ListView mCityList = (ListView) findViewById(R.id.province_list);
- //顯示進度條
- ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
- //添加參數
- HashMap<String, String> properties = new HashMap<String, String>();
- properties.put("byProvinceName", getIntent().getStringExtra("province"));
- WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getSupportCity", properties, new WebServiceCallBack() {
- @Override
- public void callBack(SoapObject result) {
- ProgressDialogUtils.dismissProgressDialog();
- if(result != null){
- cityStringList = parseSoapObject(result);
- mCityList.setAdapter(new ArrayAdapter<String>(CityActivity.this, android.R.layout.simple_list_item_1, cityStringList));
- }else{
- Toast.makeText(CityActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
- }
- }
- });
- mCityList.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- Intent intent = new Intent(CityActivity.this, WeatherActivity.class);
- intent.putExtra("city", cityStringList.get(position));
- startActivity(intent);
- }
- });
- }
- /**
- * 解析SoapObject對象
- * @param result
- * @return
- */
- private List<String> parseSoapObject(SoapObject result){
- List<String> list = new ArrayList<String>();
- SoapObject provinceSoapObject = (SoapObject) result.getProperty("getSupportCityResult");
- for(int i=0; i<provinceSoapObject.getPropertyCount(); i++){
- String cityString = provinceSoapObject.getProperty(i).toString();
- list.add(cityString.substring(0, cityString.indexOf("(")).trim());
- }
- return list;
- }
- }
接下來就是點擊相對應的城市調用WebService接口來獲取該城市下面的天氣詳情啦,為了簡單起見,我用一個TextView來顯示天氣信息,因為天氣信息很多,一個屏幕顯示不完,所以我們考慮在外面加一個ScrollView來進行滾動
- <?xml version="1.0" encoding="UTF-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <ScrollView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/weather"
- android:textColor="#336598"
- android:textSize="16sp"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
- </ScrollView>
- </RelativeLayout>
Activity的代碼就不做過多說明,跟上面的大同小異
- package com.example.webservicedemo;
- import java.util.HashMap;
- import org.ksoap2.serialization.SoapObject;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.webservicedemo.WebServiceUtils.WebServiceCallBack;
- /**
- * 顯示天氣的Activity
- *
- *
- * @author xiaanming
- *
- */
- public class WeatherActivity extends Activity{
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.weather_layout);
- init();
- }
- private void init() {
- final TextView mTextWeather = (TextView) findViewById(R.id.weather);
- ProgressDialogUtils.showProgressDialog(this, "數據加載中...");
- HashMap<String, String> properties = new HashMap<String, String>();
- properties.put("theCityName", getIntent().getStringExtra("city"));
- WebServiceUtils.callWebService(WebServiceUtils.WEB_SERVER_URL, "getWeatherbyCityName", properties, new WebServiceCallBack() {
- @Override
- public void callBack(SoapObject result) {
- ProgressDialogUtils.dismissProgressDialog();
- if(result != null){
- SoapObject detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");
- StringBuilder sb = new StringBuilder();
- for(int i=0; i<detail.getPropertyCount(); i++){
- sb.append(detail.getProperty(i)).append("\r\n");
- }
- mTextWeather.setText(sb.toString());
- }else{
- Toast.makeText(WeatherActivity.this, "獲取WebService數據錯誤", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- }
到這裡我們就完成了編碼工作,在運行程序之前我們需要在AndroidManifest.xml注冊Activity,以及添加訪問網絡的權限
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.webservicedemo.MainActivity"
- android:label="@string/title_activity_main" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".CityActivity"/>
- <activity android:name=".WeatherActivity"></activity>
- </application>
- <uses-permission android:name="android.permission.INTERNET"/>
運行結果:
省份,城市列表可以加上A-Z的排序功能,可以參考下http://www.fengfly.com/plus/view-214530-1.html,我這裡就不添加了,需要添加的朋友自行實現
源碼下載
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
寫這篇文章,做份備忘,簡單滴展示一個帶進度條的Webview示例,進度條位於Webview上面. 示例圖如下: 主Activity代碼: 代碼如下: packa
簡介 在Android中運用了很多機制,例如:廣播、服務、數據庫、通知、包……等等。 什麼是廣播?Broadcast是一種廣泛運用的在應用