android 请求网络异步载入
/**
* 封装ProecssDialog对话框
*
*/
public class LoadDialog extends ProgressDialog {
private String title = "进度对话框";
private String message = "载入数据中....";
public LoadDialog(Context context, int theme) {
super(context, theme);
} /**
* 用默认的标题和内容来创建对话框
* @param context
*/
public LoadDialog(Context context) {
super(context);
initDialog();
}
/**
* 用指定的标题和内容来创建对话框
* @param context
* @param title
* @param message
*/
public LoadDialog(Context context,String title,String message){
super(context);
if(title != null){
this.title = title;
}
if(message != null){
this.message = message;
}
initDialog();
}
/**
* 初始化对话框參数,默认对话框不能够取消
*/
public void initDialog(){
setTitle(title);
setMessage(message);
setProgressStyle(ProgressDialog.STYLE_SPINNER);
setCancelable(false);
}
/**
* 打开对话框。设置回调方法,传递须要运行业务方法的类模板,方法名和參数列表
* @param callback 回调方法,该方法在对话框关闭后回调,并获取返回的数据
* @param serviceClass 运行业务方法的类模板
* @param method 运行业务方法的方法名
* @param params 运行业务方法的參数列表
*/
public void execute(Callback callback,Class serviceClass,String method,Object... params){
super.show();
ServiceAysnTask task = new ServiceAysnTask(callback,serviceClass,method);
task.execute(params);
} /**
* 回调方法的接口
*
*/
public interface Callback{
public void getResult(Map map);
} /**
* 与远程服务通信的线程类
* @author BDK
* AsyncTask 异步任务
*/
private class ServiceAysnTask extends AsyncTask<Object,Object,Map>{
private Class serviceClass;
private String method;
private Callback callback;
public ServiceAysnTask(Callback callback,Class serviceClass,String method){
this.callback = callback;
this.serviceClass = serviceClass;
this.method = method;
}
@Override
protected Map doInBackground(Object... params) {
Map resultMap = null;
try {
Object obj = serviceClass.newInstance();//创建类模板对象
Class [] paramTypes = new Class[params.length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = params[i].getClass();
}
//依据类模板得到方法
Method m = serviceClass.getMethod(method, paramTypes);
resultMap = (Map) m.invoke(obj, params);
} catch (Exception e) {
e.printStackTrace();
}
LoadDialog.this.cancel();
return resultMap;
}
@Override
protected void onPostExecute(Map result) {
super.onPostExecute(result); if(result == null){
Toast.makeText(LoadDialog.this.getContext(), "网络通信异常", Toast.LENGTH_LONG).show();
return;
}
callback.getResult(result);
}
}
}
android 请求网络异步载入的更多相关文章
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- android 请求网络 和 httpclient的使用上传下载
访问网络最主要的也就是 http协议了. http协议很简单,但是很重要. 直接上代码了,里面都是1个代码块 代码块的,用哪一部分直接拷出去用就好了. 1.访问网络用 get 和 post 自己组拼 ...
- Android利用Volley异步载入数据完整具体演示样例(二)
MainActivity例如以下: package cc.y; import android.app.Activity; import android.content.Context; import ...
- Android请求网络权限
1,新建一个项目,在AndroidManiifest中添加 <uses-permission android:name="android.permission.INTERNET&quo ...
- Android 使用图片异步载入框架Universal Image Loader的问题
使用的Jar包 问题: optionsm = new DisplayImageOptions.Builder() .displayer(new RoundedBitmap ...
- Android中的异步网络请求
本篇文章我们来一起写一个最基本的Android异步网络请求框架,借此来了解下Android中网络请求的相关姿势.由于个人水平有限,文中难免存在疏忽和谬误,希望大家可以指出,谢谢大家:) 1. 同步网络 ...
- Android okHttp网络请求之Get/Post请求
前言: 之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里 ...
- Android okHttp网络请求之Retrofit+Okhttp+RxJava组合
前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...
- 基于Retrofit+RxJava的Android分层网络请求框架
目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...
随机推荐
- ubantu MongoDB安装
转 https://blog.csdn.net/flyfish111222/article/details/51886787 本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的 ...
- C# 调用带有输出参数的分页存储过程
一.创建带有输出参数的分页存储过程 use StudentMISDB go select * from Course alter table Course go --update Course set ...
- ArcGIS 坐标系 整理
刚使用ArcGIS的时候,对坐标系的点一直很混乱,今天想要整理整理. 一.地理坐标系与投影坐标系的区分 首先要能区分地理坐标系(GCS)和投影坐标系(PCS). 上面的是地理坐标系的举例,简单理解为不 ...
- Codeforces_766_C_(dp)
C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- mysqlworkbench 执行update语句报错:You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY colum ...
- CAD使用SetxDataLong写数据(网页版)
主要用到函数说明: MxDrawEntity::SetxDataLong 写一个long扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 n ...
- 并发和多线程(七)--volatile
volatile: 相当于轻量级的synchronized,只能用来修饰变量,线程安全的三个特性通过volatile能实现其中的两个 原子性: 在之前的文章有说到,通过Atomic相关类.synchr ...
- bazel和TensorFlow安装
bazel安装:https://docs.bazel.build/versions/master/install-ubuntu.html#install-with-installer-ubuntu 安 ...
- 剑指offer---最小的K个数
题目:最小的K个数 要求:输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. class Solution { public: ...
- Linux---文件目录管理
1. Linux文件目录架构 Linux的目录结构与win的目录有很大不同,首先,没有盘符的概念:然后Linux使用斜杠/标识目录,Linux首先建立一个根目录,然后将其他文件系统挂载到这个目录下. ...