Android网络请求开源库Android技术知识

NoHttp的使用以及封装

2016-08-20  本文已影响2459人  Luke_单车

上一篇写到OkHttp,简单的对比了下咱们安卓开发中用到的网络框架,到了android 6.0也不会支持volley了,所以了解并学习新的框架是非常有必要的,

· 在studio中进行依赖关联,在application中实现初始化,并添加相应的权限
compile 'com.yolanda.nohttp:nohttp:1.0.4'
在Application中onCreate的进行    NoHttp.initialize(this);
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

新建一个队列用来添加消息请求,可以并发(一起发送,一起请求)多个消息请求,默认为3个
发送消息请求,并添加到队列中
设置结果回调监听,对请求结果进行统一处理
[跟volley还是有点相似的]

GET:
public void getNohttp(View view) {
    RequestQueue requestQueue = NoHttp.newRequestQueue();
    final Request<String> request = NoHttp.createStringRequest("https://www.baidu.com/",
      RequestMethod.GET);
    /**
     *  what:请求的标识
     *  request:请求
     *  response:请求的回调监听
     */
    requestQueue.add(0, request, new OnResponseListener<String>() {
        @Override
        public void onStart(int what) {
        }
        @Override
        public void onSucceed(int what, Response<String> response) {
            contenttv.setText(response.get());
        }
        @Override
        public void onFailed(int what, String url, Object tag, Exception exception, int
           responseCode, long networkMillis) {
        }
        @Override
        public void onFinish(int what) {
        }
    });
}
POST:
    POST请求就是在创建消息请求的时候把RequestMethod.GET换成RequestMethod.POST
    然后
    request.add("username", "admin");
    request.add("password", "123456");
    当然也可以添加头部
    request.addHeader("xxid","123456abc");
    request.addHeader("xxkey","123456abc");
 XML就是两个button按钮和textview显示返回的网络数据

也可以参考这个

/**
   * Created  Lukey on 2016/8/20
   */
public class NoHttpManager<T> implements OnResponseListener<T> {
    private NoHttpListener<T> mListener;
    private boolean isLoading;
    private Request<T> mRequest;
    private Dialog mWaitDialog;
    public NoHttpManager(Context context, Request<T> request, NoHttpListener<T>
 httpListener, boolean canCancle, boolean isLoading) {
        mListener = httpListener;
        this.isLoading = isLoading;
        this.mRequest = request;
        if (context != null) {
            mWaitDialog = new SpotsDialog(context,"正在加载中...");
            mWaitDialog.setCancelable(canCancle);
        }
    }
    @Override
    public void onStart(int what) {
        if (isLoading && mWaitDialog != null && !mWaitDialog.isShowing()) {
            mWaitDialog.show();
        }
    }
    @Override
    public void onSucceed(int what, Response<T> response) {
        if (mListener != null) {
            mListener.onSucceed(what, response);
        }
    }
    @Override
    public void onFailed(int what, String url, Object tag, Exception exception, int
 responseCode, long networkMillis) {
        if (mListener != null) {
            mListener.onFailed(what, url, tag, exception, responseCode, networkMillis);
        }
    }
    @Override    public void onFinish(int what) {
        if (isLoading && mWaitDialog != null && mWaitDialog.isShowing()) {
            mWaitDialog.cancel();
        }
   }}
/**
 * Created  Lukey on 2016/8/20
 * 定义一个NoHttpListener接口,只需要定义成功和失败
 */
public interface NoHttpListener<T> {
    void onSucceed(int what, Response<T> response);
    void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis);}
/**
 * Created  Lukey on 2016/8/20
 * 单例模式 -- 提供外部调用进行网络请求
 */
public class NoHttpCallBack {
    //单例模式
    private final RequestQueue mQueue;
    private static NoHttpCallBack callService;
    private NoHttpCallBack() {
        mQueue = NoHttp.newRequestQueue();
    }
    public synchronized static NoHttpCallBack getInstance() {
        if (callService == null) {
            callService = new NoHttpCallBack();
        }
        return callService;
    }
    /**
     * 添加一个请求到队列中的
     */
    public <T> void add(Context context, int what, Request<T> request,
                        NoHttpListener<T> httpListener, Boolean canCanclem, Boolean
            isLoading){
        mQueue.add(what,request,new NoHttpManager<T>(context,request,
           httpListener,canCanclem,isLoading));
    }}
效果图
nohttp.gif

上面是简单的对NoHttp网络请求框架进行轻量级的封装,后期还会进行持续维护

上一篇下一篇

猜你喜欢

热点阅读