OkHttp + Retrofit使用示例。从引入依赖,编写接口,到发起网络请求。
https://github.com/square/okhttp
引入依赖,使用Retrofit2。
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填充可以用@Path
注解。
例如下面这个post请求。
@POST("user-service/user/{uid}/token/refresh")
Call<RefreshTokenResp> refreshToken(@Path("uid") String uid, @Query("token") String token);
public interface CfgService {
@GET("backend-service/config")
Call<ServerCfgResp> getServerCfg(@Query("env") String env);
}
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类型。
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证书(正式环境不建议这么做)。
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; // 强制返回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);
}
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) {
//...
}
});
并不推荐这么做
public class SSLUtils {
/**
* @return 信任所有服务器
*/
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 {
//do nothing,接受任意客户端证书
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
//do nothing,接受任意服务端证书
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
} catch (Exception e) {
}
return tm;
}
}
import io.reactivex.Observable; // 这个是rx2的包
// ---
/**
* 用户反馈接口
*
* @param content 用户输入的反馈内容
*/
@POST("feedbackAction")
Observable<UserFeedback> userFeedback(@Query("appVersion") String appVersion,
@Query("phoneModel") String phoneModel,
@Query("phoneOsVersion") String osVersion,
@Query("submitContent") String content);
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'
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;
/**
* RustDrone后台接口
* Created on 2019-5-17
*/
public interface RustDroneCommonService {
/**
* 用户反馈接口
*
* @param content 用户输入的反馈内容
*/
@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);
/**
* 获取手机验证码
*
* @param mobile 手机号
*/
@GET("verifyCode")
Observable<PhoneCodeResp> getPhoneCode(@Query("mobile") String mobile, @Query("oprType") int type);
}
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) {
// LL.dn(TAG, "onSubscribe: " + 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: 上传结束");
}
});
代码地址: https://github.com/RustFisher/GithubOnAndroid
// 仅仅是示例,不做任何处理
private Interceptor doNothingInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Log.d(TAG, "Interceptor1: intercept");
return chain.proceed(chain.request());
}
};
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。
// 添加一些公共参数
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 取消请求的方法