美文网首页工作生活
OkHttp请求数据封装类

OkHttp请求数据封装类

作者: MoooortyTrue | 来源:发表于2019-07-03 10:11 被阅读0次

Util类:

public class HttpUtil {

      static HttpUtilhttpUtil;

      OkHttpClient okHttpClient;

      private HttpUtil(){

              okHttpClient=new OkHttpClient();

       }
      //获取单利
      public static HttpUtilgetInstance(){

            if (httpUtil==null) {
                    synchronized (HttpUtil.class)
                          {
                                  if (httpUtil==null)
                                        {
                                            httpUtil=new HttpUtil();
                                        }
                          }
              }
              return httpUtil;

        }
  public void getData(String url, final HttpRespon respon){

        Request request =new Request.Builder()

        .url(url)

        .build();

        //开启一个异步请求

        okHttpClient.newCall(request).enqueue(new Callback() {

        @Override
         public void onFailure(Call call, IOException e) {

                            e.printStackTrace();

                           respon.onError("请求服务器失败");

            }

            @RequiresApi(api = Build.VERSION_CODES.KITKAT)

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

                              try (ResponseBody responseBody = response.body()) {

                                    if (!response.isSuccessful()) {
                                      //请求失败

                                      respon.onError("请求服务器失败");
                                            return;
                    }

                    String date=responseBody.string();

                respon.parse(date);

                                    }
                              }

                        });

             }

}

Respon类

public abstract class HttpRespon {

Class tClass;

public HttpRespon(Class cls){

              this.tClass=cls;

    }

public abstract void onError(String msg);

public abstract void onSuccess(T t);

public void parse(String json){

if (TextUtils.isEmpty(json))

      {

//请求失败

            onError("连接失败");

        return;

            }

          if (tClass== String.class)

                {

                onSuccess((T)json);

                  return;

        }

T result = JsonUtil.parseJson(json,tClass);

        if (result!=null)

          {

          onSuccess(result);

          return;

                    }

      else {

onError("json解析失败");

            }

      }

}

Json解析类:

public class JsonUtil {

    static GsonmGson;

    public static T parseJson(String json, Class tClass){

    if(mGson ==null){

          mGson =new Gson();

      }

    if(TextUtils.isEmpty(json)){

              return null ;

        }

        return mGson.fromJson(json,tClass);

    }

}

使用示例:

HttpUtil instance = HttpUtil.getInstance();

instance.getData(url, new HttpRespon(LearnRecord.class) {

    @Override

    public void onError(String msg) {

            Log.d(TAG,msg);

    }

    @Override

    public void onSuccess(LearnRecord learnRecord) {

        String status = learnRecord.getStatus();

        Log.d(TAG,"status为"+status);

        String title = learnRecord.getRecordDetials().get(0).getTitle();

        Log.d(TAG,"title为"+title);

    }

});

相关文章

网友评论

    本文标题:OkHttp请求数据封装类

    本文链接:https://www.haomeiwen.com/subject/vxnfhctx.html