本文最后更新于:2023年4月15日 下午
本文链接
OkHttp + Retrofit使用示例。从引入依赖,编写接口,到发起网络请求。
https://github.com/square/okhttp
简单使用
引入依赖
引入依赖,使用Retrofit2。
1 2 3
| implementation 'com.squareup.retrofit2:retrofit:2.1.0' implementation 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
|
查询 @Query
例如URL https://base_url/backend-service/config?env=dev
,问号后面属于查询内容。
不论是GET或POST,都要用@Query
这个注解。否则会报异常。
URL填充与拼接
单纯URL填充可以用@Path
注解。
例如下面这个post请求。
1 2
| @POST("user-service/user/{uid}/token/refresh") Call<RefreshTokenResp> refreshToken(@Path("uid") String uid, @Query("token") String token);
|
GET带有查询的参数
1 2 3 4 5
| public interface CfgService {
@GET("backend-service/config") Call<ServerCfgResp> getServerCfg(@Query("env") String env); }
|
POST,带有查询的参数和body
1 2 3 4 5 6 7 8 9
| public interface UserService {
@POST("user-service/login") Call<LoginResp> login(@Query("lenovoST") String token, @Query("realm") String realm, @Body RequestBody body);
@POST("user-service/logout") Call<CommonEntity> logout(@Query("token") String token, @Body RequestBody body); }
|
调用的时候要创建RequestBody
;先调查好后台接受的body类型。
1 2 3 4 5 6 7
| Map<String, String> map = new HashMap<>(); map.put("system", "Android"); map.put("phoneBrand", Build.BRAND); map.put("modelNum", Build.MODEL); Gson gson = new Gson(); String bodyJson = gson.toJson(map); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), bodyJson);
|
初始化OkHttpClient
;这里信任所有的SSL证书(正式环境不建议这么做)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private CfgService cfgService;
public void initService() { SSLSocketFactory sslSocketFactory = null; try { sslSocketFactory = SSLUtils.getSSLSocketFactory(); } catch (Exception e) { e.printStackTrace(); } OkHttpClient.Builder builder = new OkHttpClient.Builder(); if (sslSocketFactory != null) { Log.d(TAG, "sslSocketFactory != null"); builder.sslSocketFactory(sslSocketFactory); } else { Log.w(TAG, "sslSocketFactory == null"); } builder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); OkHttpClient lenClient = builder.build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(ServerCfg.HOST_URL) .client(lenClient) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); cfgService = retrofit.create(CfgService.class); }
|
调用网络请求
1 2 3 4 5 6 7 8 9 10 11
| mNetworkManager.getUserApi().login(mLenovoToken, ServerCfg.RID, requestBody).enqueue(new Callback<LoginResp>() { @Override public void onResponse(Call<LoginResp> call, final Response<LoginResp> response) { }
@Override public void onFailure(Call<LoginResp> call, final Throwable t) { } });
|
信任所有服务器的ssl
并不推荐这么做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class SSLUtils {
public static SSLSocketFactory getSSLSocketFactory() throws Exception { SSLSocketFactory sslSocketFactory = null; SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{createTrustAllManager()}, new SecureRandom()); sslSocketFactory = sslContext.getSocketFactory(); return sslSocketFactory; }
public static X509TrustManager createTrustAllManager() { X509TrustManager tm = null; try { tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; } catch (Exception e) {
} return tm; }
}
|
service使用io.reactivex.Observable
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import io.reactivex.Observable;
@POST("feedbackAction") Observable<UserFeedback> userFeedback(@Query("appVersion") String appVersion, @Query("phoneModel") String phoneModel, @Query("phoneOsVersion") String osVersion, @Query("submitContent") String content);
|
示例1 - Retrofit2,RxJava2
引入依赖
1 2 3 4 5
| implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
|
定义interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import java.util.Map;
import io.reactivex.Observable; import retrofit2.http.Field; import retrofit2.http.FieldMap; import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query;
public interface RustDroneCommonService {
@FormUrlEncoded @POST("feedbackAction") Observable<FeedbackResp> userFeedback(@Field("appVersion") String appVersion, @Field("phoneModel") String phoneModel, @Field("phoneOsVersion") String osVersion, @Field("submitContent") String content, @FieldMap Map<String, String> map);
@GET("verifyCode") Observable<PhoneCodeResp> getPhoneCode(@Query("mobile") String mobile, @Query("oprType") int type); }
|
调用接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers;
RustDroneDataCenter.getCenter().getCommonService().userFeedback(BuildConfig.VERSION_NAME, Build.MODEL.replace(" ", "-"), Build.VERSION.RELEASE, fd, ext) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<FeedbackResp>() { @Override public void onSubscribe(Disposable d) {
}
@Override public void onNext(FeedbackResp feedbackResp) { LL.dn(TAG, "onNext: " + feedbackResp); if (feedbackResp.getCode() == 0) { popSubmitSuccessDialog(); } else { LL.e("上传用户反馈失败"); mPbLayout.setVisibility(View.GONE); popRetryDialog(); } }
@Override public void onError(Throwable e) { LL.e("上传用户反馈失败 code: " + e); mPbLayout.setVisibility(View.GONE); popRetryDialog(); }
@Override public void onComplete() { mPbLayout.setVisibility(View.GONE); LL.dn(TAG, "onComplete: 上传结束"); } });
|
GithubOnAndroid示例
代码地址: https://github.com/RustFisher/GithubOnAndroid
添加拦截器
定义拦截器
1 2 3 4 5 6 7 8
| private Interceptor doNothingInterceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Log.d(TAG, "Interceptor1: intercept"); return chain.proceed(chain.request()); } };
|
添加拦截器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| OkHttpClient okHttpClient = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .addInterceptor(doNothingInterceptor) .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Log.d(TAG, "Interceptor2: intercept"); return chain.proceed(chain.request()); } }) .addNetworkInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Log.d(TAG, "NetworkInterceptor1: intercept"); return chain.proceed(chain.request()); } }) .build();
|
操作chain中的request。
1 2 3 4 5 6 7 8 9 10 11
|
private Interceptor RustDroneInterceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("token", UserCenter.getToken()) .build(); return chain.proceed(request); } };
|
更多请参考:
Android OkHttp + Retrofit 使用示例
Android OkHttp + Retrofit 取消请求的方法
Android OkHttp + Retrofit 下载文件与进度监听
Android OkHttp + Retrofit 断点续传