Retrofit2 使用经验
来源:互联网 发布:义乌最好的淘宝摄影 编辑:程序博客网 时间:2024/06/05 04:59
基本使用方法
1 准备工作
build.gradle:
dependencies { // Retrofit & OkHttp compile 'com.squareup.retrofit2:retrofit:2.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.0' }说明:Retrofit2.0默认使用okhttp进行网络处理,不用单独添加。除非你需要特定的版本
2 定义ServiceGenerator:
public class ServiceGenerator { public static final String API_BASE_URL = "http://your.api-base.url"; private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S> serviceClass) { Retrofit retrofit = builder.client(httpClient.build()).build(); return retrofit.create(serviceClass); } }
3 定义请求接口:
GET:
public interface GitHubClient { @GET("/repos/{owner}/{repo}/contributors") Call<List<Contributor>> contributors( @Path("owner") String owner, @Path("repo") String repo );}public class Contributor { String login; int contributions;}
4 发起网络请求
public static void main(String... args) { // Create a very simple REST adapter which points the GitHub API endpoint. GitHubClient client = ServiceGenerator.createService(GitHubClient.class); // Fetch and print a list of the contributors to this library. Call<List<Contributor>> call = client.contributors("fs_opensource", "android-boilerplate"); try { List<Contributor> contributors = call.execute().body(); } catch (IOException e) { // handle errors } for (Contributor contributor : contributors) { System.out.println( contributor.login + " (" + contributor.contributions + ")"); }}
进阶《一》
GET方法:
@Path:路径参数@Query:?后面的参数,例如:?expand="dddddd"@GET("users/{user_id}/activities/{name}/reward") Call<ResponseBody> putReward( @Path("user_id") String user_id, @Path("name") String name, @Query("expand") String expand );
POST方法:
@FormUrlEncoded @POST("users/{user_id}/activities/{huodong_id}/bj") Call<ResponseBody> huodongSample( @Path("user_id") String user_id, @Path("huodong_id") String activity_id, @Field("code") String code ); @Field:Post传递的参数 @FormUrlEncoded:如果POST请求,传递数据,必须要有
统一处理的一些逻辑:
没有网络的处理:
Retrofit2.0无网状态处理: 无网会回调onFailure(Call<ResponseBody> call, Throwable t)方法,在该方法内部调用showFailure即可
public void showFailure( Throwable t) { if(t.getMessage() != null && t.getMessage().startsWith("Failed to connect to")){ Log.d("lincoln","当前网络情况不稳定"); } }
每次请求都添加Header或者Token:
添加intercept即可
httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); Request request = original.newBuilder() .header("User-Agent", "Your-App-Name") .header("Accept", "application/vnd.yourapi.v1.full+json") .method(original.method(), original.body()) .build(); return chain.proceed(request); }}
0 0
- Retrofit2 使用经验
- Retrofit2使用
- Retrofit2使用
- Retrofit2.0使用详解
- Retrofit2.0使用
- Retrofit2.0的使用
- Retrofit2.0使用
- Retrofit2.0 的使用
- Retrofit2.0使用详解
- Retrofit2.0使用详解
- Retrofit2简单使用
- Retrofit2.0使用详解
- Retrofit2.0使用详解
- Retrofit2.0使用详解
- Retrofit2 使用记录
- Retrofit2.0使用
- Retrofit2.0使用小结
- Retrofit2.0的使用
- 文件拷贝速度比较
- 3d基础图形原理
- phaser.js显示对象篇
- The APK file does not exist on disk
- spring整合mybatis
- Retrofit2 使用经验
- Helper2416开发板移植minigui3.0.12之二:移植到开发板
- 友元 成员 非成员 点间距离
- 13. php数据库抽象层PDO(二)
- 防止机器注册
- rabbitmq中消息的存储
- jQ源码之camelCase
- 认识算法-利用循环
- LoadRunner监控Windows和Linux常见问题