Java 技术IT/互联网Android网络

仿京东商城系列3------封装Okhttp

2017-08-22  本文已影响473人  小庄bb

本项目来自菜鸟窝,有兴趣者点击http://www.cniao5.com/course/

项目已经做完,源码地址。https://github.com/15829238397/CN5E-shop

仿京东商城系列0------项目简介
仿京东商城系列1------fragmentTabHost实现底部导航栏
仿京东商城系列2------自定义toolbar
仿京东商城系列3------封装Okhttp
仿京东商城系列4------轮播广告条
仿京东商城系列5------商品推荐栏
仿京东商城系列6------下拉刷新上拉加载的商品列表
仿京东商城系列7------商品分类页面
仿京东商城系列8------自定义的数量控制器
仿京东商城系列9------购物车数据存储器实现
仿京东商城系列10------添加购物车,管理购物车功能实现
仿京东商城系列11------商品排序功能以及布局切换实现(Tablayout)
仿京东商城系列12------商品详细信息展示(nativie与html交互)
仿京东商城系列13------商品分享(shareSDK)
仿京东商城系列14------用户登录以及app登录拦截
仿京东长城系列15------用户注册,SMSSDK集成
仿京东商城系列16------支付SDK集成
仿京东商城系列17------支付功能实现
仿京东商城系列18------xml文件读取(地址选择器)
仿京东商城系列19------九宫格订单展示
仿京东商城系列20------终章


前言

本文讲述Okhttp的封装过程,便于使用,不对源码部分进行深究。
推荐一篇文章
okhttp源码学习笔记(一)-- 综述

内容

基本使用

 <uses-permission android:name="android.permission.INTERNET"/>
     compile 'com.squareup.okhttp:okhttp-urlconnection:2.7.5'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
 OkHttpClient client = new OkHttpClient() ;
        client.setConnectTimeout(5 , TimeUnit.SECONDS);
        client.setWriteTimeout(5 , TimeUnit.SECONDS);
        client.setReadTimeout(5 , TimeUnit.SECONDS);

        String url = "http://www.jianshu.com/p/6ffde18fb034" ;

        Request request = new Request.Builder().url(url).get().build() ;
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                
            }

            @Override
            public void onResponse(Response response) throws IOException {

            }
        });
    }
 OkHttpClient client = new OkHttpClient() ;
        client.setConnectTimeout(5 , TimeUnit.SECONDS);
        client.setWriteTimeout(5 , TimeUnit.SECONDS);
        client.setReadTimeout(5 , TimeUnit.SECONDS);

        String url = "http://www.jianshu.com/p/6ffde18fb034" ;
        //盛放参数
        RequestBody requestBody = new FormEncodingBuilder()
                .add("name","xxx").build() ;

        Request request = new Request.Builder().url(url).post(requestBody).build() ;
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {

            }
        });
    }

okhttp封装

由上面的简单使用,我们可以知道Okhttp的使用有几个重要的部分:

package com.example.cne_shop.okhttp;

import android.app.Service;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.example.cne_shop.myException.GET_RESPONSE_MESSAGE_FAILURE;
import com.example.cne_shop.myException.GSON_ANALYZE_MESSAGE_FAILURE;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by 博 on 2017/8/22.
 */

public class OkhttpHelper {

    //所有的请求公用一个OkhttpClient
    private static OkHttpClient client ;
    private Handler handler ;
    private Gson gson ;

    private static final int TOKEN_ERROR = 401 ;
    private static final int TOKEN_EXPRISE = 402 ;
    private static final int TOKEN_MISSING = 403 ;

    private OkhttpHelper(){

        //初始化client信息
        client = new OkHttpClient() ;
        client.setConnectTimeout(5 , TimeUnit.SECONDS);
        client.setWriteTimeout(5 , TimeUnit.SECONDS);
        client.setReadTimeout(5 , TimeUnit.SECONDS);

        gson = new Gson() ;
    }

    //提供一个静态方法供外部请求。
    public static OkhttpHelper getOkhttpHelper(){
        return new OkhttpHelper() ;
    }

    //提交get请求的方法。
    public void doGet (String uri , BaseCallback callback){
        doGet(uri , callback , null);
    }

    //带参数请求的方法
    public void doGet (String uri , BaseCallback callback , Map<String , String> formData){
        Request request = buildRequest(uri , METHOD_TYPE.GET , formData);
        doRequest(request , callback);
    }

    public void doPost(String uri , BaseCallback callback , Map<String , String> formData){
        Request request = buildRequest(uri , METHOD_TYPE.POST , formData);
        doRequest(request , callback);
    }

    private void doRequest(final Request request , final BaseCallback callback){

        callback.onRequestBefore();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                //提交失败
                callbackFailure(callback , request ,e);
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()){
                    String jqury = response.body().string() ;

                    if (callback.type == String.class){
                        callback.callBackSucces(response , jqury);
                    }else {
                        try{
                            Object object = gson.fromJson(jqury , callback.type) ;
                            callbackSuccess(callback , response , jqury);
                        }catch (Exception e){
                            callbackError(callback , response , null);
                            throw new GSON_ANALYZE_MESSAGE_FAILURE("gson解析信息失败") ;
                        }
                    }
                }else{
                    callbackError(callback , response , null);
                }

            }
        });


    }

    private Request buildRequest (String uri , METHOD_TYPE method_type , Map<String , String> formData){

        Request.Builder builder = new Request.Builder() ;
        if(method_type == METHOD_TYPE.GET){

            uri = getUriWithParams(uri , formData) ;
            builder.url(uri) ;
            builder.get() ;

        }else if (method_type == METHOD_TYPE.POST){
            builder.url(uri) ;
            RequestBody requestBody = buildFormData(formData) ;
            builder.post(requestBody) ;
        }
        return builder.build() ;
    }

    private RequestBody buildFormData (Map<String , String> formData){

        if(formData != null) {
            FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder() ;
            for (Map.Entry<String , String> objectMap : formData.entrySet()){
                formEncodingBuilder.add( objectMap.getKey() , objectMap.getValue() ) ;
            }

            return formEncodingBuilder.build() ;
        }
        return null ;
    }

    //对参数进行处理
    private String getUriWithParams(String uri , Map<String , String > formData){
        String symbol = null ;
        int signNum = 0 ;

       if(formData == null){
           return uri ;
       }

        for(String key : formData.keySet()){
            symbol = (signNum++ == 0 )? "?" : "&" ;
            uri = uri+symbol+key+"="+formData.get(key) ;
        }
        return uri ;
    }

    /**
     * 以下方法保证对于okhttp的拦截处理运行在主线程
     *
     */
    private void callbackTokenError (final BaseCallback baseCallback , final Response response){
        handler.post(new Runnable() {
            @Override
            public void run() {
                baseCallback.onTokenError(response , response.code());
            }
        });
    }

    private void callbackSuccess (final BaseCallback baseCallback , final Response response , final Object object){
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    baseCallback.callBackSucces(response , object);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void callbackError (final BaseCallback baseCallback , final Response response , final Object object){
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    baseCallback.onErroe(response , response.code() , new GET_RESPONSE_MESSAGE_FAILURE("获取服务器信息失败"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void callbackFailure (final BaseCallback baseCallback , final Request request , final IOException e){
        handler.post(new Runnable() {
            @Override
            public void run() {
                baseCallback.onFailure(request , e);
            }
        });
    }



    enum METHOD_TYPE {
        GET,
        POST
    }

    private void demo(){

        OkHttpClient client = new OkHttpClient() ;
        client.setConnectTimeout(5 , TimeUnit.SECONDS);
        client.setWriteTimeout(5 , TimeUnit.SECONDS);
        client.setReadTimeout(5 , TimeUnit.SECONDS);

        String url = "http://www.jianshu.com/p/6ffde18fb034" ;
        //盛放参数
        RequestBody requestBody = new FormEncodingBuilder()
                .add("name","xxx").build() ;

        Request request = new Request.Builder().url(url).post(requestBody).build() ;
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {

            }
        });
    }

}

其中BaseCallBack类代码如下:

package com.example.cne_shop.okhttp;

import com.google.gson.internal.$Gson$Types;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by 博 on 2017/8/22.
 */

public abstract class BaseCallback<T> {
    public Type type ;

    static Type getSuperclassTypeParameter(Class<?> subclass ){
        Type superClass = subclass.getGenericSuperclass() ;
        if (superClass instanceof Class){
            throw new RuntimeException("Missing type parameter") ;
        }
        ParameterizedType parameterizedType = (ParameterizedType) superClass;
        return $Gson$Types.canonicalize(parameterizedType.getActualTypeArguments()[0]) ;
    }



    public BaseCallback(){
        this.type = getSuperclassTypeParameter(this.getClass()) ;
    }

    public abstract void onRequestBefore();
    public abstract void onFailure(Request request, IOException e) ;
    public abstract void onErroe(Response response , int responseCode , Exception e) throws IOException ;
    public abstract void callBackSucces(Response response , T t) throws IOException ;
    public abstract void onTokenError(Response response , int responseCode );

}

使用

上一篇下一篇

猜你喜欢

热点阅读