1.this libaray is base onAsyncTask,AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

2.Iencapsulate http request params into Request . you can access the cloud like this:

finalRequest request =newRequest(url);// link url , the default constructor is GET request

request.addHeader("accept", "application/json"); // set request headers

request.addPostParams(key, value); // set post content

request.setRequestListener(newIRequestListener() {// when the AsyncTask execute , it will callback let code to handle something

@Override

publicvoidonProgressUpdate(intstatus,intcurPos,inttotalPos) {// callback to update UI progress

@Override

publicvoidonPreExecute() { // callback , this is in UI thread , you may show progress or do sth prepare

}

@Override

publicvoidonPostExecute() { // callback , this is in UI thread , you may show the tips that http has done

}

@Override

publicvoidonPrepareParams(OutputStream out) { // callback , you can use OutputStream to push the post content yourself

}

@Override

publicvoidonCancelled() { // callback , this is in UI thread , you may show that the http connection has been cancelled

}

@Override

publicvoidonBeforeDoingBackground() { // callback , this is in Sub thread , you may do sth preparation , like query db , prepare the post params.

}

@Override

publicObject onAfterDoingBackground(Object object) { // callback , this is in Sub thread, you may do sth that insert/update db , create files .etc

returnnull;

}

});

request.setCallback(newJsonCallback<Companies>() {// callback , this is in UI thread , will return the data that http response.

@Override

publicvoidonCallback(Companies callback) {// the callback can be DTOs , path that write data into file , or just String data

for(Company company : callback.companies) {

Trace.d(company.toString());

}

assertTrue(callback.companies.size() > 0);

}

@Override

publicvoidonFailure(Exception e) {// e can be CloudException , AbortException or other RuntimeException

thrownewAssertionError(e.toString());

}

}.setReturnType(Companies.class)

.setFilePath(path) // if you want save the data into file first , you can setFilePath(path).

);// setReturnType , if you want callback to return you the exactly DTO , pls set the Class and use JsonCallback , it will deserialize automatically , and return object to you

request.execute();// execute the code to http access. if you just for test ,pls call request.test() . this method is only for test .

首先分析下需求。

在Android里,我们常常要通过网络去获取数据,然后更新UI,网络请求当然要放到子线程里,然后更新UI在主线程,所以基于这样的需求,Android才提供了AsyncTask,所以我们再基于AsyncTask再做一次封装,这样,网络请求就变的方便的多。你再也不用烦躁于线程切换,重复写代码了。一个好车轮能让你省很多时间。

首先定义一个Request类,把所有可能用到的参数define在里面,比如callback(服务器返回的数据),params(push到服务器的data),listener(下载进度,执行过程)。。。

网络请求有Get Post。。为了低耦合,基本都要拆开,比如我会用HttpClient 也可能用HttpUrlConnection.那么写个HttpUtil用来封装http的请求代码。这里定义一个execute()方法,在这个方法里判断是get还是post请求,再去call get() post() 方法,那么post请求需要push data到服务器,所以你要把params设置到Request中,有可能你需要push file到服务器,所以我添加了一个回调方法,把OutputStream传回去,手动的把file flush到Stream中。

在AsyncTask中,在doingBackground()方法中execute http,然后再调用AbstractCallback预先处理服务器返回的InputStream,如果data不大,可以直接写到内存变成String返回,如果data比较大,建议写到tmp文件里,把path返回,在不同的callback里再处理。所以就会有不同的callback,StringCallback PathCallback JsonCallback 你也可以根据你的特殊需求来定义不同的callback,你也可以在callback里将数据deserialize到database中。这样创建一个网络请求代码就会非常轻松。

贴个AbstractCallback处理流的方法,因为我没装代码插件,大家先凑合看,改天我加上了就会好很多了。

public Object handleConnection(HttpURLConnection response,

IProgressListener task) throws Exception {

try {

checkIsCancelled();

int statusCode = response.getResponseCode();

long contentLength = response.getContentLength();

InputStream in = null;

if (statusCode == HttpStatus.SC_OK) {

String encoding = response.getContentEncoding();

if (encoding != null && "gzip".equals(encoding))

in = new GZIPInputStream(response.getInputStream());

else if (encoding != null && "deflate".equals(encoding))

in = new InflaterInputStream(response.getInputStream());

else

in = response.getInputStream();

// if set path , write the data into file

if (!TextUtils.isEmpty(path)) {

FileOutputStream fos = new FileOutputStream(path);

byte[] b = new byte[IO_BUFFER_SIZE];

int curPos = 0;

int read;

while ((read = in.read(b)) != -1) {

checkIsCancelled();

if (task != null) {

curPos += read;

task.onProgressUpdate(

IProgressListener.DOWNLOADING,

curPos / 1000, contentLength == -1 ? -1

: (int) (contentLength / 1000));

}

fos.write(b, 0, read);

}

fos.flush();

fos.close();

in.close();

return bindData(path, task);

} else {

return bindData(

new String(IOUtilities.readStreamToMemory(in)), task);

}

} else {

throw new CloudException(statusCode,

response.getResponseMessage(), new String(

IOUtilities.readStreamToMemory(response

.getErrorStream())));

}

} finally {

if (response != null) {

response.disconnect();

response = null;

}

}

}

写框架,你必须要考虑全面,想所有不同的可能性,但是要有个度,全部想要封装也是很难的,所以基于不同的项目,框架也会发生改变。大家也不要迷信于某个框架能满足你所有的需求。你应该造你需要的轮子,能跑的最快的轮子。

源代码改天再提供。大家可以先自己考虑考虑,搭框架不难,主要是要有个全局观。也就是代码写的多了,烦了,想偷懒了,框架雏形就出来了。

更多相关文章

  1. Android(安卓)中 Bitmap 和 Drawable 相互转换的方法
  2. android安卓手机分屏多窗口实现方法
  3. android 关于listview item设置高度的问题解决方法
  4. Day03
  5. Android的常用方法(转载)
  6. @BindView问题 Attempt to invoke virtual method 'void android
  7. Android(安卓)Vibrate
  8. Android(安卓)Studio 中setOnClickListener出错
  9. Android(安卓)使用 HTTPS

随机推荐

  1. android 两个listView 共存的显示问题
  2. Android下Handler分析
  3. 如何使用MPAndroidChart中的LineChart实
  4. Android(安卓)Studio --> Gradle Build设
  5. Android(安卓)查询数据库时 项目出现 OOM
  6. Android(安卓)SDK 下载安装及配置
  7. Android(安卓)自定义方形验证码输入框,仿
  8. Android(安卓)蓝牙操作详解
  9. 使用DialogFragment遇到的一些问题
  10. Xposed模块开发Cannot load module问题