Android retrofit使用

//retrofit 的json解析器
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//Gson解析的依赖
compile 'com.google.code.gson:gson:2.8.1'


public interface KingApi {

    //保存地址
    @FormUrlEncoded
    @POST("addresssave")
//    Call uploadData(@Field("userid") int header, @Body MineBean bean);
    Call uploadData(@Header("userid") int userid , @Field("addressArea") String addressArea,
                              @Field("addressDetail") String addressDetail,
                              @Field("city") String city,
                              @Field("isDefault") int isDefault,
                              @Field("name") String name,
                              @Field("phoneNumber") String phoneNumber,
                              @Field("province") String province,
                              @Field("zipCode") String zipCode);
//    Call getData(@Body MineBean bean);
//    Call getData(@Header("userid"));

    //模拟登陆使用zengjiawei
    @FormUrlEncoded
    @POST("login")
    Call zjwLogin(@Field("username") String username, @Field("password") String password);

    //获取地址列表zengjiawei
//    @FormUrlEncoded
    @GET("addresslist")
    Call addressList(@Header("userid") int userid);
}


public class JDMallRetrofit {
    private static JDMallRetrofit sJDMallRetrofit;

    private final KingApi mKingApi;
    private final Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    private JDMallRetrofit() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.HOST)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        mKingApi = retrofit.create(KingApi.class);
    }

    public static JDMallRetrofit getInstance() {
        if (sJDMallRetrofit == null) {
            synchronized (JDMallRetrofit.class) {
                if (sJDMallRetrofit == null) {
                    sJDMallRetrofit = new JDMallRetrofit();
                }
            }
        }
        return sJDMallRetrofit;
    }

    public KingApi getKingApi() {
        return mKingApi;
    }

}


public class Constants {

   public static final String HOST = "https://www.hzjr.com/Mobile/";
}

返回的bean对象格式不对会造成请求错误

                        Call call = JMRetrofit.getInstance().getKingApi().addressDeleteData(Integer.valueOf(mUserid), bean.getId());
                        call.enqueue(new Callback() {
                            @Override
                            public void onResponse(Call call, Response response) {
                            }

                            @Override
                            public void onFailure(Call call, Throwable throwable) {
                            }
                        });

Retrofit各个注解的含义及作用

Cookie获取

此方法会失效,不推荐使用

retrofit回调中获得的就是cookie,要去除分号后面的无用数据,看后台接受什么
Headers headers = response.headers();
String cookie = headers.get("Set-Cookie");

SaveCookiesInterceptor(正确获取方式)

public class SaveCookiesInterceptor implements Interceptor {
    private Context mContext;

    public SaveCookiesInterceptor(Context context) {
        mContext = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response originalResponse = chain.proceed(request);
        String cookie = originalResponse.header(Constance.COOKIE);

        if (!originalResponse.headers(Constance.COOKIE).isEmpty()) {
//            HashSet cookies = new HashSet<>();
//            List headers = originalResponse.headers("Set-Cookie");
//
//            for (String header : originalResponse.headers("Set-Cookie")) {
//              cookies.add(header);
//            }

            SPUtils.putString(mContext, Constance.COOKIE, cookie);
//            Preferences.getDefaultPreferences().edit()
//                    .putStringSet(Preferences.PREF_COOKIES, cookies)
//                    .apply();

        }

        return originalResponse;
    }
}

然后在retrofit的单例中添加

测试发现okHttpClient 同时添加saveCookie和addCookie的拦截器是不奏效的,所以这里不用addCookie

 private HzjrRetrofit() {

        LoadDataUtil.setCookieListener(new LoadDataUtil.CookieListener() {
            @Override
            public void cookieChange() {
                cookie=preferencesUtil.getString(Constants.mContext,SharedPreferencesUtil.SET_COOKIE,"");
            }
        });

        SaveCookiesInterceptor.setCookieListener(new SaveCookiesInterceptor.CookieListener() {
            @Override
            public void cookieChange() {
                cookie=preferencesUtil.getString(Constants.mContext,SharedPreferencesUtil.SET_COOKIE,"");
            }
        });


        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(Constants.mContext));
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new SaveCookiesInterceptor(Constants.mContext))
                .addInterceptor(new AddCookiesInterceptor(SharedPreferencesUtil.getInstance().getString(Constants.mContext,
                        SharedPreferencesUtil.SET_COOKIE,"")))
                .cookieJar(cookieJar)
                //其他配置
                .build();

        /*OkHttpClient client = new OkHttpClient.Builder().build();
        client.interceptors().add(new SaveCookiesInterceptor(Constants.mContext));(最优方案)
        client.interceptors().add(new AddCookiesInterceptor(SharedPreferencesUtil.getInstance().getString(Constants.mContext,"COOKIE","")));*/


        Retrofit retrofit = new Retrofit.Builder()
//                .client(okHttpClient)
                .client(genericClient())
//                .client(client)
                .baseUrl(Constants.HOST)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        mHzjrApi = retrofit.create(HzjrApi.class);
    }

    public static OkHttpClient genericClient() {
        LogUtil.showLog("www=genericClient","i am here");
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                               // .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Content-Type", "application/json; charset=UTF-8")
注释下行 否则请求网络返回数据乱码
                                .addHeader("Accept-Encoding", "gzip, deflate")
                                .addHeader("Connection", "keep-alive")
                                .addHeader("Accept", "*/*")
                                .addHeader("Cookie", cookie)
                                .build();
                        return chain.proceed(request);
                    }

                })
                .build();

        return httpClient;
    }

AddCookiesInterceptor

public class AddCookiesInterceptor implements Interceptor {
    private String mCookie;

    public AddCookiesInterceptor(String cookie) {
        mCookie = cookie;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        //Request.Builder builder = chain.request().newBuilder();
       // HashSet preferences = (HashSet) Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet<>());
//        for (String cookie : preferences) {
//            builder.addHeader("Cookie", cookie);
//            Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
//        }
        Request request = chain.request();
        Response originalResponse = chain.proceed(request);
        CacheControl.Builder builder = new CacheControl.Builder().maxAge(10, TimeUnit.MINUTES);

        return originalResponse.newBuilder()
                .header("Cache-Control", builder.build().toString())
                .addHeader("cookie", mCookie)
                .build();
    }
}

retrofit 2.2.0 上传头像

@Multipart
@POST("Member/cropImg")//上传图片
Call uploadImg(@Part("token") RequestBody token,
                           @Part("timestamp") RequestBody timestamp,
        @Part MultipartBody.Part file);



final File file = new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);


MultipartBody.Part body =
        MultipartBody.Part.createFormData("avatarImg", file.getName(), requestBody);

RequestBody token =
        RequestBody.create(
                MediaType.parse("multipart/form-data"), timestampAndToken.get(1));
RequestBody time =
        RequestBody.create(
                MediaType.parse("multipart/form-data"), timestampAndToken.get(0));


Call uploadImg = HzjrRetrofit.getInstance().getHzjrApi().uploadImg(token, time, body);

请求复用

首先要明确一点:每个Call实例可以且只能执行一次请求,不能使用相同的对象再次执行execute()或enqueue()。


但是,很多时候我们都需要再次执行请求,例如初次请求超时失败,这时候就要刷新再次请求。如果我们需要再次执行请求,可以使用call.clone()方法来创建call的一个副本,这个副本与call包含相同的配置,我们可以使用这个副本来再次执行相同的请求。代码示例如下:


WeiboService weiboService = ServiceGenerator.createService(WeiboService.class);
Call call = weiboService.timelineForPublic(COUNT_PER_REQ, page);
call.enqueue(callback);

Call newCall = call.clone();
newCall.enqueue(callback);

取消请求
很多时候,当用户离开了当前页面,该页面中尚未完成的请求就没有必要了,这时候我们就需要取消请求了。在Retrofit中实现非常简单,使用call.cancel()来取消请求。

取消很简单,但是我们也应该处理取消之后的后续操作。当一个请求取消时,回调方法onFailure()会执行,而onFailure()方法在没有网络或网络错误的时候也会执行。这两种情况下我们的处理是不一样的,如果没有网络,我们会告诉用户连接网络,而取消请求则有可能只取消进度条。因此,需要对这两种情况进行区分,代码如下:

@Override
public void onFailure(Call call, Throwable t) {
    if (call.isCanceled()) {
        DebugLog.i(TAG, "request is canceled");
    } else {
        DebugLog.i(TAG, "error:" + t.getMessage());
    }
}
通过call.isCanceled()方法来判断是否请求取消,从而对不同的情况分别处理。

你可能感兴趣的:(Android retrofit使用)