Android程序最重要的模块就是网络部分,如何从网络上下载数据,如何将处理过的数据上传至网络,往往是android程序的关键环节。


       Android原生提供基于HttpClient和HttpUrlConnection的两种网络访问方式。利用原生的这两种方式编写网络代码,需要自己考虑很多,获取数据或许可以,但是如果要将手机本地数据上传至网络,根据不同的web端接口,需要组织不同的数据内容上传,给手机端造成了很大的工作量。


       目前有几种快捷的网络开发开源框架,给我们提供了非常大的便利,他们应该是android网络处理部分开源框架的前三名。


       1. https://github.com/loopj/android-async-http




            loopj这款开源框架一直是我钟爱的网络框架,在我写过的相当多的程序中,一直使用该框架,以至于对这个框架产生了厌倦的情绪。


            太详细的使用文档,太简单的使用方式,它就像是一门高级语言,定制好了所有的东西,你只需要拼凑一下就能够处理,所以你想定制更加细节的操作,只有自己动手,反而造成了一些不必要的麻烦。


            loopj在前一段时间对https的支持并不好,如果要支持https,则需要费点功夫。


            1.4几个版本的升级对前部分代码的承接性不够好,造成了我对这个框架的厌倦。


            


                    


                    获取一张图片:


 


AsyncHttpClient client = new AsyncHttpClient(); client.get("http://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) { @Override public void onSuccess(int statusCode, Header[] headers, File response) { // Do something with the file `response` } });构造请求参数:RequestParams params = new RequestParams(); params.put("key", "value"); params.put("more", "data");

        



 


            对于Loopj来说,HttpRequest就是一种简单粗暴,一个请求可以涵盖在一句代码当中,不给任何一点冗余的成分。



String response = HttpRequest.get("http://google.com").body();



                    将数据打印到控制台,或者其他的流:   



HttpRequest.get("http://google.com").receive(System.out);



                    忽略https的安全:


                    


HttpRequest request = HttpRequest.get("https://google.com"); //Accept all certificates request.trustAllCerts(); //Accept all hostnames


request.trustAllHosts();



            对于小项目,这种网络框架简直就是一种幸福的选择。


                    


 


       3. https://github.com/square/okhttp   




            


            相比于loopj和HttpRequest,okhttp要更为强大,使用方式也稍显复杂,但是却是我目前最喜欢的感觉。


            这是著名公司Square的开源框架,出手不凡。


            下面贴出我项目中的代码:


          


OkHttpClient client = new OkHttpClient(); client.networkInterceptors().add(new StethoInterceptor()); RequestBody body = new FormEncodingBuilder() .add("data", data).add("token", getToken(data)) .add("system", "sample").build(); Request request = new Request.Builder() .url("http://sample.com/api/v1") .post(body).build(); Response response = client.newCall(request).execute(); final String reslut = response.body().string();


 

     在上述代码中有这样一行代码:


client.networkInterceptors().add(new StethoInterceptor());

这是使用facebook的开源stetho来进行查看网络访问情况的一个工具。



okhttp可以通过chrome的开发工具来查看手机的网络访问情况,就和web开发人员一样可以在浏览器中查看手机访问网络的实时信息,查看提交和返回结果,大大的方便了开发,这个工具的使用将会专门写一篇博客来进行讲解。

android 网络工具大全 android常用网络框架_开源框架


package com.peiandsky.chromedebug;


import java.io.IOException;


import com.facebook.stetho.okhttp.StethoInterceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;


public class Net {
    private static final boolean debug = true;
    private static OkHttpClient okHttpClient = new OkHttpClient();
    static {
        if (debug) {
            okHttpClient.networkInterceptors().add(new StethoInterceptor());
        }
    }


    public static final void askBaidu() {
        Request request = new Request.Builder().url("http://www.baidu.com")
                .build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            String reslut = response.body().string();


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}