Android OkHttp + Retrofit 使用示例

热门标签

, ,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

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填充与拼接

单纯URL填充可以用@Path注解。
例如下面这个post请求。

@POST("user-service/user/{uid}/token/refresh")
Call<RefreshTokenResp> refreshToken(@Path("uid") String uid, @Query("token") String token);

GET带有查询的参数

public interface CfgService {

    @GET("backend-service/config")
    Call<ServerCfgResp> getServerCfg(@Query("env") String env);
}

POST,带有查询的参数和body

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) {
                //...
            }
        });

信任所有服务器的ssl

并不推荐这么做

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;
    }

}

service使用io.reactivex.Observable

mport 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);

示例1 - Retrofit2,RxJava2

引入依赖

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

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: 上传结束");
                        }
                    });

GithubOnAndroid示例

代码地址: 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();

拦截器中增加header

操作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 取消请求的方法

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

Android OkHttp + Retrofit 断点续传

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《Android OkHttp + Retrofit 使用示例》 发布于2019-11-01

分享到:
赞(0) 打赏

评论 抢沙发

4 + 2 =


Android OkHttp + Retrofit 使用示例

长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?