最好的安卓网络请求库: Fast Android Networking

原文:The Best Android Networking Library for Fast and Easy Networking

1-PNHTc2Hbk3bmu0GZGx5Vmw.png 

最近我发布了一个library,我认为它是安卓上处理网络的最简方式。

以下是Fast Android Networking优于它库的几个方面:

每个请求都可以轻易的定制OkHttpClient-比如超时等。

因为它使用了OkHttpClientOkio,所以速度很快

支持RxJava点击这里

支持把JSON解析成java对象(同时支持Jackson解析)。

可以得到任何请求的详细数据分析。你可以知道发送了多少字节,接收了多少字节,以及请求所花的时间。这些数据是很重要的,这样你就可以识别出慢的请求。

你可以得到当前的带宽和网络质量以写出更合理的代码-网络质量高的时候下载高质量的图片,不好的时候下载低质量的图片。

Proper Response Caching— which leads toreduced bandwidth usage.

很好的响应请求-可以减少带宽的使用。

一个executor可以被传递给任何请求让响应在另一个线程中被获取。如果你在响应中做了很重的任务,你不可以在主线程中做。

一个库就能处理所有类型的网络问题-下载,上传,multipart。

可以通过设置prefetch(预取)让请求在需要的时候立即返回缓存。

任何类型的自定义都是可以的

立即请求真的是立即发生的。

很好的取消请求。

如果一个请求完成了指定的百分比,可以阻止其取消。

一个简单的接口就可以做任何类型的请求。

为什么你应该使用Fast Android Networking Library?

最近Android Marshmallow(Android M)上对HttpClient的移除让其它的网络库都过时了。

没有哪个单一的库做完了所有的事情,比如发送请求,下载任意类型的文件,上传文件,在ImageView中加载网络图片,等等。有一些库可以但是都过时了。

因为使用了sOkHttp,所以支持HTTP/2。

没有哪个库为网络中所有类型的事情提供了简单的接口,比如设置优先级,取消请求等。

如何使用Fast Android Networking

build.gradle

compile 'com.amitshekhar.android:android-networking:0.2.0'

如果和RxJava一起使用

compile 'com.amitshekhar.android:rx-android-networking:0.1.0'

如果和Jackson Parser一起使用

compile 'com.amitshekhar.android:jackson-android-networking:0.2.0'

别忘了加网络权限

<uses-permission android:name="android.permission.INTERNET" />

然后在application类的onCreate()方法中做初始化:

AndroidNetworking.initialize(getApplicationContext());

 发起GET请求

AndroidNetworking.get("http://api.localhost.com/{pageNumber}/test")
                 .addPathParameter("pageNumber", "0")
                 .addQueryParameter("limit", "3")
                 .addHeaders("token", "1234")
                 .setTag("test")
                 .setPriority(Priority.LOW)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                    // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error
                    }
                });

发起POST请求

AndroidNetworking.post("http://api.localhost.com/createAnUser")
                 .addBodyParameter("firstname", "Amit")
                 .addBodyParameter("lastname", "Shekhar")
                 .setTag("test")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                    // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error
                    }
                });

和JAVA对象一起使用 - JSON Parser

/*--------------Example One -> Getting the userList----------------*/
AndroidNetworking.get("http://api.localhost.com/getAllUsers/{pageNumber}")
                .addPathParameter("pageNumber", "0")
                .addQueryParameter("limit", "3")
                .setTag(this)
                .setPriority(Priority.LOW)
                .build()
                .getAsParsed(new TypeToken<List<User>>() {}, new ParsedRequestListener<List<User>>() {
                    @Override
                    public void onResponse(List<User> users) {
                    // do anything with response
                    Log.d(TAG, "userList size : " + users.size());
                    for (User user : users) {
                        Log.d(TAG, "id : " + user.id);
                        Log.d(TAG, "firstname : " + user.firstname);
                        Log.d(TAG, "lastname : " + user.lastname);
                    }
                    }
                    @Override
                    public void onError(ANError anError) {
                     // handle error
                    }
                });
/*--------------Example Two -> Getting an user----------------*/
AndroidNetworking.get("http://api.localhost.com/getAnUser/{userId}")
                .addPathParameter("userId", "1")
                .setTag(this)
                .setPriority(Priority.LOW)
                .build()
                .getAsParsed(new TypeToken<User>() {}, new ParsedRequestListener<User>() {
                     @Override
                     public void onResponse(User user) {
                     // do anything with response
                         Log.d(TAG, "id : " + user.id);
                         Log.d(TAG, "firstname : " + user.firstname);
                         Log.d(TAG, "lastname : " + user.lastname);
                     }
                     @Override
                     public void onError(ANError anError) {
                      // handle error
                     }
                 }); 
/*-- Note : TypeToken and getAsParsed is important here --*/

从服务器下载一个文件

AndroidNetworking.download(url,dirPath,fileName)
                 .setTag("downloadTest")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .setDownloadProgressListener(new DownloadProgressListener() {
                    @Override
                    public void onProgress(long bytesDownloaded, long totalBytes) {
                    // do anything with progress  
                    }
                 })
                 .startDownload(new DownloadListener() {
                    @Override
                    public void onDownloadComplete() {
                    // do anything after completion
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error    
                    }
                });

上传一个文件

AndroidNetworking.upload(url)
                 .addMultipartFile("image",file)    
                 .setTag("uploadTest")
                 .setPriority(Priority.IMMEDIATE)
                 .build()
                 .setUploadProgressListener(new UploadProgressListener() {
                    @Override
                    public void onProgress(long bytesUploaded, long totalBytes) {
                    // do anything with progress 
                    }
                 })
                 .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                    // do anything with response                
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error 
                    }
                 });

得到一个响应并在另一个线程executor中完成

(注 : 错误和进度总是在主线程中返回)

AndroidNetworking.upload(url)
                 .addMultipartFile("image",file)    
                 .setTag("uploadTest")
                 .setPriority(Priority.IMMEDIATE)
                 .build()
                 .setExecutor(Executors.newSingleThreadExecutor()) // setting an executor to get response or completion on that executor thread
                 .setUploadProgressListener(new UploadProgressListener() {
                    @Override
                    public void onProgress(long bytesUploaded, long totalBytes) {
                    // do anything with progress 
                    }
                 })
                 .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                    // below code will be executed in the executor provided
                    // do anything with response                
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error 
                    }
                 });

取消一个请求

任何一个指定了tag的请求都可以被取消。

AndroidNetworking.cancel("tag");

把从网络加载的图片放到ImageView

      <com.androidnetworking.widget.ANImageView
          android:id="@+id/imageView"
          android:layout_width="100dp"
          android:layout_height="100dp"
          android:layout_gravity="center" />
      imageView.setDefaultImageResId(R.drawable.default);
      imageView.setErrorImageResId(R.drawable.error);
      imageView.setImageUrl(imageUrl);

从url获取指定了一些参数的Bitmap

AndroidNetworking.get(imageUrl)
                 .setTag("imageRequestTag")
                 .setPriority(Priority.MEDIUM)
                 .setBitmapMaxHeight(100)
                 .setBitmapMaxWidth(100)
                 .setBitmapConfig(Bitmap.Config.ARGB_8888)
                 .build()
                 .getAsBitmap(new BitmapRequestListener() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                    // do anything with bitmap
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error
                    }
                });

Prefetch a request (so that it can return from cache when required at instant)

AndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY)
                .addPathParameter("pageNumber", "0")
                .addQueryParameter("limit", "30")
                .setTag(this)
                .setPriority(Priority.LOW)
                .build()
                .prefetch();

为某个请求自定义OkHttpClient

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .addInterceptor(new GzipRequestInterceptor())
                .build();
AndroidNetworking.get("http://api.localhost.com/{pageNumber}/test")
                 .addPathParameter("pageNumber", "0")
                 .addQueryParameter("limit", "3")
                 .addHeaders("token", "1234")
                 .setTag("test")
                 .setPriority(Priority.LOW)
                 .setOkHttpClient(okHttpClient) // passing a custom okHttpClient 
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                    // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error
                    }
                });

用ConnectionClass Listener得到当前网络质量和带宽

// Adding Listener
AndroidNetworking.setConnectionQualityChangeListener(new ConnectionQualityChangeListener() {
            @Override
            public void onChange(ConnectionQuality currentConnectionQuality, int currentBandwidth) {
                // do something on change in connectionQuality
            }
        });
// Removing Listener   
AndroidNetworking.removeConnectionQualityChangeListener();
// Getting current ConnectionQuality
ConnectionQuality connectionQuality = AndroidNetworking.getCurrentConnectionQuality();
if(connectionQuality == ConnectionQuality.EXCELLENT){
// do something
}else if (connectionQuality == ConnectionQuality.POOR){
// do something
}else if (connectionQuality == ConnectionQuality.UNKNOWN){
// do something
}
// Getting current bandwidth
int currentBandwidth = AndroidNetworking.getCurrentBandwidth(); // Note : if (currentBandwidth == 0) : means UNKNOWN

Getting Analytics of a request by setting AnalyticsListener on that

AndroidNetworking.download(url,dirPath,fileName)
                 .setTag("downloadTest")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .setAnalyticsListener(new AnalyticsListener() {
                      @Override
                      public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
                          Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                          Log.d(TAG, " bytesSent : " + bytesSent);
                          Log.d(TAG, " bytesReceived : " + bytesReceived);
                          Log.d(TAG, " isFromCache : " + isFromCache);
                      }
                  })
                 .setDownloadProgressListener(new DownloadProgressListener() {
                    @Override
                    public void onProgress(long bytesDownloaded, long totalBytes) {
                    // do anything with progress  
                    }
                 })
                 .startDownload(new DownloadListener() {
                    @Override
                    public void onDownloadComplete() {
                    // do anything after completion
                    }
                    @Override
                    public void onError(ANError error) {
                    // handle error    
                    }
                });  
Note : If bytesSent or bytesReceived is -1 , it means it is unknown

更多例子以及 Fast Android Networking Library 项目在这里

我们需要开发者为 Fast Android Networking library贡献一份力量。

和RxJava一起使用Fast Android Networking请看这里

完整信息请看我们网站上的文档:

Website Link

谢谢阅读。