OkHttp,Retrofit 1.x - 2.x 基本使用

  Square 为广大开发者奉献了OkHttp,Retrofit1.x,Retrofit2.x,运用比较广泛,这三个工具有很多相似之处,初学者可能会有一些使用迷惑。这里来总结一下它们的一些基本使用和一些细微差别。

/**************
Retrofit 基本使用方法

Retrofit 到底是返回什么? void, Observable, Call?

*************/
/********************************************Retrofit****************************************************************/
/*** 同步调用的方式  ****/
interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  List<Contributor> repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo);
} 

List<Contributor> contributors =
    gitHubService.repoContributors("square", "retrofit");
/***** 异步调用的方式 仅限于 Retrofit 1.x !!!!!!! *****/
interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  void repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo,
      Callback<List<Contributor>> cb); // 异步调用添加 CallBack
} 

service.repoContributors("square", "retrofit", new Callback<List<Contributor>>() {
  @Override void success(List<Contributor> contributors, Response response) {
    // ...
  }

  @Override void failure(RetrofitError error) {
    // ...
  }
});

/**** Rxjava 方式 ****/
interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  Observable<List<Contributor>> repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo);
}
// 调用
gitHubService.repoContributors("square", "retrofit")
    .subscribe(new Action1<List<Contributor>>() {
      @Override public void call(List<Contributor> contributors) {
        // ...
      }
    });

/*******************************注意以下三个Callback的不同***************************************/

// Retrofit Callback Version 1.9
public interface Callback<T> {

  /** Successful HTTP response. */
  void success(T t, Response response);

  /**
   * Unsuccessful HTTP response due to network failure, non-2XX status code, or unexpected
   * exception.
   */
  void failure(RetrofitError error);
}
// Retrofit Callback Version 2.0	!!!!!!!!!
public interface Callback<T> {
  /** Successful HTTP response. */
  void onResponse(Response<T> response, Retrofit retrofit);

  /** Invoked when a network or unexpected exception occurred during the HTTP request. */
  void onFailure(Throwable t);
}
// OkHttp
public interface Callback {
  void onFailure(Request request, IOException e);

  void onResponse(Response response) throws IOException; // 注意参数不同
}

/*********************************回顾一下Okhttp的调用方式********************************************/

//1. 创建
OkHttpClient : OkHttpClient client = new OkHttpClient();
//2. 创建
Request :  Request request = new Request.Builder()
									    .url("https://api.github.com/repos/square/okhttp/issues")
										.header("User-Agent", "OkHttp Headers.java")
										.addHeader("Accept", "application/json; q=0.5")
										.addHeader("Accept", "application/vnd.github.v3+json")
										.build();

//3. 使用 client 执行请求(两种方式):
//第一种,同步执行
Response response = client.newCall(request).execute();
// 第二种,异步执行方式
client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, Throwable throwable) {
    // 复写该方法

    }
    @Override public void onResponse(Response response) throws IOException {
	// 复写该方法
    }
}

/***********************************Retrofit 1.0 不能获得 Header 或者整个 Body*****************************************/
/**********引入 Call , 每个Call只能调用一次,可以使用Clone方法来生成一次调用多次,使用Call既可以同步也可以异步*********/

interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  Call<List<Contributor>> repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo);
}

Call<List<Contributor>> call =
    gitHubService.repoContributors("square", "retrofit");

response = call.execute(); /*************** 同步的方式调用,注意这里返回了 Response 后面会提到 ********************/

// This will throw IllegalStateException: 每个Call只能执行一次
response = call.execute();

Call<List<Contributor>> call2 = call.clone(); // 调用Clone之后又可以执行
// This will not throw:
response = call2.execute();

/************************ 异步的方式调用 *********************************/

Call<List<Contributor>> call =
    gitHubService.repoContributors("square", "retrofit");

call.enqueue(new Callback<List<Contributor>>() {
  @Override void onResponse(/* ... */) {
    // ...
  }

  @Override void onFailure(Throwable t) {
    // ...
  }
});

/****************************引入 Response,获取返回的RawData,包括:response code, response message, headers**********************************/

class Response<T> {
  int code();
  String message();
  Headers headers();

  boolean isSuccess();
  T body();
  ResponseBody errorBody();
  com.squareup.okhttp.Response raw();
}

interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  Call<List<Contributor>> repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo);
} 

Call<List<Contributor>> call =
    gitHubService.repoContributors("square", "retrofit");
Response<List<Contributor>> response = call.execute(); 

/*********************************** Dynamic URL *****************************************/

interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  Call<List<Contributor>> repoContributors(
      @Path("owner") String owner,
      @Path("repo") String repo);

  @GET
  Call<List<Contributor>> repoContributorsPaginate(
      @Url String url);// 直接填入 URL 而不是在GET中替换字段的方式
}

/*************************************根据返回值实现重载*****************************************************/
interface SomeService {
  @GET("/some/proto/endpoint")
  Call<SomeProtoResponse> someProtoEndpoint(); // SomeProtoResponse

  @GET("/some/json/endpoint")
  Call<SomeJsonResponse> someJsonEndpoint(); // SomeJsonResponse
}

interface GitHubService {
  @GET("/repos/{owner}/{repo}/contributors")
  Call<List<Contributor>> repoContributors(..);

  @GET("/repos/{owner}/{repo}/contributors")
  Observable<List<Contributor>> repoContributors2(..);

  @GET("/repos/{owner}/{repo}/contributors")
  Future<List<Contributor>> repoContributors3(..); // 可以返回 Future
}

/******************************************Retrofit 1.x Interceptor,添加头部信息的时候经常用到Interceptor*************************************************************/
    RestAdapter.Builder builder = new RestAdapter.Builder().setRequestInterceptor(new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json;versions=1");
        }
    });

/******************************************Retrofit 2.x Interceptor**************************************************/            

OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
                                  .header("Accept", "application/json")
                                  .header("Authorization", "auth-token")
                                  .method(original.method(), original.body())
                                  .build();

       Response response = chain.proceed(request);
       return response;      

    }
}

Retrofit retrofit = Retrofit.Builder()
            .baseUrl("https://your.api.url/v2/")
            .client(client).build();

/***************************************异步实例*********************************************/
public interface APIService {

    @GET("/users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);

    @GET("/users/{user}/repos")
    Call<String> listReposStr(@Path("user") String user);
//错误,不能这样使用异步
//    @GET("/users/{user}/repos")
//    void listRepos(@Path("user") String user, Callback<List<Repo>> callback);
}

private void prepareServiceAPI() {
    //For logging
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new MyInterceptor());
    client.interceptors().add(logging);
	// setUp Retrofit
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.github.com")
            //.addConverterFactory(new ToStringConverterFactory())
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();

    service = retrofit.create(APIService.class);
}
// 异步调用
public void execute() {
    Call<List<Repo>> call = service.listRepos("pasha656");
    call.enqueue(new Callback<List<Repo>>() {
        @Override
        public void onResponse(Response<List<Repo>> response, Retrofit retrofit) {

            if (response.isSuccess()) {
                if (!response.body().isEmpty()) {
                    StringBuilder sb = new StringBuilder();
                    for (Repo r : response.body()) {
                        sb.append(r.getId()).append(" ").append(r.getName()).append(" \n");
                    }
                    activity.setText(sb.toString());
                }
            } else {
                APIError error = ErrorUtils.parseError(response, retrofit);
                Log.d("Pasha", "No succsess message "+error.getMessage());
            }

            if (response.errorBody() != null) {
                try {
                    Log.d("Pasha", "Error "+response.errorBody().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("Pasha", "onFailure "+t.getMessage());
        }
    });
}

  

时间: 2024-08-03 04:48:11

OkHttp,Retrofit 1.x - 2.x 基本使用的相关文章

OkHttp+ Retrofit使用从0开始(一)

几种网络框架的比较 1.volley 一个简单的http异步请求库,但不支持同步,不能post大数据(上传文件时有问题): 2.android-async-http 和volley一样,是异步的请求库,只不过volley使用的是httpUrlConnection,而它使用的是HttpClient.这个库已经不再适合Android; 3.okhttp 基于httpUrlConnection,支持同步和异步,但需要自己再封装下: 4.retrofit: 对 okhttp再次封装,在项目中可以直接使用

Android OkHttp + Retrofit 取消请求的方法

本文链接 前言 在某一个界面,用户发起了一个网络请求,因为某种原因用户在网络请求完成前离开了当前界面,比较好的做法是取消这个网络请求.对于OkHttp来说,具体是调用Call的cancel方法. 如何找到这一个网络请求并取消掉它呢? 操作大致分为3步.第一步,在建立请求时,给请求(request)添加标记:第二步,根据标记,找到请求:最后,取消这个请求. OkHttp中的tag 要取消一个请求,OkHttp中可以使用cancel方法,参考. OkHttp的request对象有tag.可以根据ta

Android OkHttp + Retrofit 下载文件与进度监听

本文链接 下载文件是一个比较常见的需求.给定一个url,我们可以使用URLConnection下载文件. 使用OkHttp也可以通过流来下载文件. 给OkHttp中添加拦截器,即可实现下载进度的监听功能. 使用流来实现下载文件 代码可以参考:https://github.com/RustFisher/android-Basic4/tree/master/appdowloadsample 获取并使用字节流,需要注意两个要点,一个是服务接口方法的 @Streaming 注解,另一个是获取到Respo

Android 使用Okhttp/Retrofit持久化cookie的简便方式

首先cookie是什么就不多说了,还是不知道的话推荐看看这篇文章 Cookie/Session机制详解 深入解析Cookie技术 为什么要持久化cookie也不多说了,你能看到这篇文章代表你有这个需求. cookie简单来说就是服务器在客户端中保存的键值对,比如说早期的购物车,保持登陆状态都是使用的cookie. 但是cookie的功能是依赖于浏览器的,大多数浏览器都有管理cookie的功能.当然,你也能通过设置禁止掉这项功能,毕竟cookie是很容易泄露用户隐私的 上面也说了cookie功能依

[Android] 转-RxJava+MVP+Retrofit+Dagger2+Okhttp大杂烩

原文url: http://blog.iliyun.net/2016/11/20/%E6%A1%86%E6%9E%B6%E5%B0%81%E8%A3%85/ 这几年来android的网络请求技术层出不穷,网络请求从最初的HttpClient,HttpURLConnection到Volley,OkHttp,Retrofit.但是如果直接使用,每个网络请求都会重复很多相同的代码,这显然不是一个老司机需要的.接下来我们就讲讲网络请求封装那点事. 主要利用以下技术点 - Retrofit2 Retrof

Android OkHttp使用与分析

安卓开发领域,很多重要的问题都有了很好的开源解决方案,例如网络请求 OkHttp + Retrofit 简直就是不二之选."我们不重复造轮子不表示我们不需要知道轮子该怎么造及如何更好的造!",在用了这些好轮子将近两年之后,现在是时候拆开轮子一探究竟了.本文基于 OkHttp 截至 2016.7.11 的最新源码对其进行了详细分析. 1,整体思路 从使用方法出发,首先是怎么使用,其次是我们使用的功能在内部是如何实现的,实现方案上有什么技巧,有什么范式.全文基本上是对 OkHttp 源码的

Android 网络开源库之-retrofit的解析详解

前言 当前的网络开源库有许多,如volley,okhttp,retrofit等,这三个库当前是比较火的,其中,okhttp和retrofit由square团队开发.关于这三个库的区别,请移步stackoverflow或者知乎查看.开发过程中选择什么样的开源库需要更具我们APP来做出选择.我们选出stackoverflow中的一段话来看下. 上面说,需要与web service通信的时候,我们使用retrofit.百度百科 web service介绍,那么我们见天就来了解下retrofit.

这是一份很详细的 Retrofit 2.0 使用教程(含实例讲解)(转)

前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将献上一份非常详细Retrofit v2.0的使用教程,希望你们会喜欢. 如果对Retrofit v2.0的源码感兴趣,可看文章:Android:手把手带你深入剖析 Retrofit 2.0 源码 目录 1. 简介 特别注意: 准确来说,Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装. 原因:网络请求的工作本质上是 OkHttp 完成,

Retrofit 2.0 使用详细教程

文章来自:https://blog.csdn.net/carson_ho/article/details/73732076 前言 在Andrroid开发中,网络请求十分常用 而在Android网络请求库中,Retrofit是当下最热的一个网络请求库 今天,我将献上一份非常详细Retrofit v2.0的使用教程,希望你们会喜欢. 如果对Retrofit v2.0的源码感兴趣,可看文章:Android:手把手带你深入剖析 Retrofit 2.0 源码 目录 1. 简介 特别注意: 准确来说,Re