精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Android客戶端獲取服務器的json數據(一)

Android客戶端獲取服務器的json數據(一)

在上學期(大三),實驗室老師給我們一個小項目,做一個手機購物的客戶端,我負責寫服務器,服務器采用ssh+jpa,返回json數據給客戶端。但是負責寫Client的童鞋他們沒有太給力,于是,我又抱著練習的心態去寫Client。唉,往事已矣,老師說這個項目是個練習項目...結果,我們就沒有練習下去了,只做了一個半成品。逝者如斯夫,不舍晝夜,這里是為了紀念那些日子和當時用到的開發模式。

1.有一些程序截圖,UI是我的大問題啊。

  

     

2.采用mvc模式,處理Client業務與UI更新。畫不來圖,直接上代碼理解。

  ⑴定義一個IMActivity接口,聲明兩個抽象方法,項目中與UI有關的activity都要implements該接口

    public abstract void init();//實現數據初始化

    public abstract void refresh(Object ... param);//當獲取到網絡數據后,更新UI.

  ⑵開發一個業務處理中心,一個后臺Service。功能為獲取網絡數據,處理線程通信,發送更新UI的消息對象。

  ⑶定義一個任務bean,用于新建任務,任務類型:用戶登錄,獲取產品類別,獲取產品等等。

  ⑷UI設計

  ⑸獲取網絡json數據的輔助類

  

3.具體代碼過程

  ⑴根據不同的層的功能,建立程序包結構。

  

  ⑵activity接口

  

package com.mpros.service.model;/**** 本系統的所有activity父接口,實現activity初始化和Ui更新* @author Scherrer**/public interface IMActivity {public abstract void init();public abstract void refresh(Object ...param);}

  ⑶Service服務

  

View Code
package com.mpros.service;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.app.Service;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.graphics.drawable.BitmapDrawable;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import com.mpros.activity.R;import com.mpros.activity.classify.ChildClassifyActivity;import com.mpros.activity.classify.ClassifyActivity;import com.mpros.activity.classify.ProductListActivity;import com.mpros.bean.Task;import com.mpros.bean.product.ProductType;import com.mpros.service.json.ProductTypeService;import com.mpros.service.model.IMActivity;import com.mpros.service.model.ProductAction;import com.mpros.util.HttpUtil;/*** 實現業務調度的核心邏輯服務*/public class MainService extends Service implements Runnable {// 保存所有的activitypublic static ArrayList<Activity> allActivities = new ArrayList<Activity>();// 保存前一個Activity的編號public static int lastAcvityId;// 所以任務public static ArrayList<Task> allTasks = new ArrayList<Task>();// 循環控制變量private boolean isrun = true;// 保存產品類型logopublic static HashMap<Integer, BitmapDrawable> allTypeIcon = new HashMap<Integer, BitmapDrawable>();// 保存二級分類logopublic static HashMap<Integer, BitmapDrawable> allChildTypeIcon = new HashMap<Integer, BitmapDrawable>();// 保存產品logopublic static HashMap<Integer, BitmapDrawable> allProductLogo = new HashMap<Integer, BitmapDrawable>();//產品的所有樣式//public static HashMap<Integer, ArrayList<BitmapDrawable>> allProductDescImage = new HashMap<Integer, ArrayList<BitmapDrawable>>();/*** 在集合里,通過name獲取Activity對象** @param name* @return Activity*/public static Activity getActivityByName(String name) {for (Activity a : allActivities) {if (a.getClass().getName().indexOf(name) >= 0) {return a;}}return null;}/*** 新建任務** @param task*/public static void newTask(Task task) {// 添加一個任務        allTasks.add(task);}/********** 啟動線程*/@Overridepublic void run() {while (isrun) {Task lastTask = null;if (allTasks.size() > 0) {synchronized (allTasks) {// 獲取任務lastTask = allTasks.get(0);// 執行任務doTask(lastTask);}}// 如果沒有任務,則等待2000ms,繼續獲取任務try {Thread.sleep(2000);} catch (Exception e) {e.printStackTrace();}}}/************************* 很據任務ID,執行該任務** @param task*/@SuppressWarnings({ "unchecked", "rawtypes" })private void doTask(Task task) {Message msg = new Message();System.out.println("任務編號: " + task.getTaskId());msg.what = task.getTaskId();try {switch (task.getTaskId()) {case Task.TASK_GET_PRODUCTTYPE:// 獲取產品類型// 傳遞消息和數據List<ProductType> types = ProductTypeService.getTypesFromJson(ProductAction.GET_PRODUCTYPE_ACTION);if (types != null) {if (allTypeIcon == null) {allTypeIcon = new HashMap<Integer, BitmapDrawable>();}// 獲取logofor (ProductType type : types) {BitmapDrawable bd = allTypeIcon.get(type.getTypeid());if (bd == null) {HashMap param = new HashMap();param.put("typeid", type.getTypeid());param.put("typelogo", ProductAction.BASE_ACTION+ type.getTypelogo());Log.i(Task.Logger + type.getTypeid(),ProductAction.BASE_ACTION+ type.getTypelogo());Task tk = new Task(Task.GET_TYPE_LOGO, param);MainService.newTask(tk);}}msg.obj = types;}break;case Task.GET_TYPE_LOGO:Integer typeid = (Integer) task.getTaskParam().get("typeid");BitmapDrawable drawable = HttpUtil.getImageFromUrl(task.getTaskParam().get("typelogo").toString());// 添加logo到集合里                allTypeIcon.put(typeid, drawable);break;case Task.TASK_GET_CHILDTYPE_LOGO:Integer childtypeid = (Integer) task.getTaskParam().get("typeid");BitmapDrawable childdrawable = HttpUtil.getImageFromUrl(task.getTaskParam().get("typelogo").toString());// 添加logo到集合里                allChildTypeIcon.put(childtypeid, childdrawable);break;case Task.TASK_GET_PRODUCT_IMAGE:Integer productid = (Integer)task.getTaskParam().get("productid");BitmapDrawable productlogo = HttpUtil.getImageFromUrl(task.getTaskParam().get("productlogo").toString());allProductLogo.put(productid, productlogo);break;}} catch (Exception e) {msg.what = -100;e.printStackTrace();}handler.sendMessage(msg);allTasks.remove(task);// 執行完任務,則移出該任務    }// 當前服務的子線程Handler,負責處理更新UI操作private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);Log.i(Task.Logger, "UI 更新編號:" + msg.what);switch (msg.what) {case Task.TASK_GET_PRODUCTTYPE:IMActivity ia = (ClassifyActivity) getActivityByName("ClassifyActivity");ia.refresh(ClassifyActivity.GET_TYPE_SUCCESS, msg.obj);case Task.GET_TYPE_LOGO:IMActivity ia1 = (ClassifyActivity) getActivityByName("ClassifyActivity");ia1.refresh(ClassifyActivity.REFRESH_TYPE_LOGO, msg.obj);break;case Task.TASK_GET_CHILDTYPE_LOGO:IMActivity ia2 = (ChildClassifyActivity) getActivityByName("ChildClassifyActivity");ia2.refresh(ChildClassifyActivity.REFRESH_CHILDTYPE_LOGO,msg.obj);break;case Task.TASK_GET_PRODUCT_IMAGE:IMActivity ia3 = (ProductListActivity)getActivityByName("ProductListActivity");ia3.refresh(ProductListActivity.REFRESH_PRODUCT_LOGO);break;}}};@Overridepublic void onCreate() {super.onCreate();isrun = true;new Thread(this).start();}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();isrun = false;}/******************** 網絡連接出錯,提示對話框** @param context*/public static void alerNetErr(final Context context) {// 對話框AlertDialog.Builder ab = new AlertDialog.Builder(context);ab.setTitle(R.string.NoRouteToHostException);ab.setMessage(R.string.NoSignalException);// 設置操作對象        ab.setPositiveButton(R.string.apn_is_wrong1_setnet,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 取消對話框                        dialog.cancel();// 打開網絡設置ActivityIntent it = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);context.startActivity(it);}});ab.setNegativeButton(R.string.apn_is_wrong1_exit,new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 取消對話框                        dialog.cancel();// 退出程序// exitApp(context);                    }});// 顯示        ab.create().show();}/************************* 提示對話框** @param context*/public static void promptExit(final Context context) {// 通過Inflater對象把布局文件壓縮為視圖LayoutInflater flater = LayoutInflater.from(context);View exitView = flater.inflate(R.layout.exitdialog, null);AlertDialog.Builder builder = new AlertDialog.Builder(context);// 改變對話框的默認布局,用已有視圖來覆蓋        builder.setView(exitView);builder.setPositiveButton(R.string.confirm_exit,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {exitApp(context);}});builder.setNegativeButton(R.string.apn_is_wrong1_exit, null);// 顯示對話框        builder.show();}/*************** 退出程序*/public static void exitApp(Context context) {// 出棧所有Activityif (allActivities != null) {for (Activity ac : allActivities) {ac.finish();}}// 關閉服務Intent it = new Intent("com.xl.service.MainService");context.stopService(it);System.exit(0);}}

我要等一分鐘了...

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android 游戲開發,音頻/聲音操作,MediaPlayer與SoundPool
安卓動態添加刪除多個控件
Writing Parcelable classes for Android
實現Android圖片圓角
Android進階篇之引導頁系列之ViewPager實現Animation動畫引導頁
Android中利用OnTouchListener在ImageView中框選顯示圖片
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 托克逊县| 揭西县| 米脂县| 南川市| 万荣县| 土默特右旗| 永丰县| 达孜县| 兴和县| 曲阜市| 大安市| 新安县| 碌曲县| 涡阳县| 红安县| 华坪县| 板桥市| 尚义县| 太原市| 侯马市| 太仆寺旗| 高青县| 三门峡市| 龙山县| 大悟县| 瑞金市| 磐安县| 泰州市| 逊克县| 迁西县| 青州市| 凌源市| 沽源县| 武定县| 增城市| 通榆县| 南郑县| 云安县| 白城市| 金阳县| 祁门县|