• retrofit之基本笔记


    5.retrofit的基本用法:
    public interface MovieService{
    @GET("top250")
    Call getTopMovie(@Query("start")int start,@Query("count") int count);
    }
    然后你还需要创建一个Retrofit对象:
    public static final String baseUrl="https://api.douban.com/v2/movie/";
    Retrofit retrofit=new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

    再用这个Retrofit对象创建一个MovieService对象:
    MovieService movieService=retrofit.create(MovieService.class);
    Call call=movieService.getTopMovie(0,10);
    //call.enqueue()异步请求,call.execute()同步请求
    call.enqueue(new Callback(){
    @Override
    public void onResponse(Call call, Response response) {
    result_TV.setText(response.body().toString());
    }
    @Override
    public void onFailure(Call call, Throwable t) {
    result_TV.setText(t.getMessage());
    }
    });
    在这边还可以移除一个请求:
    Retrofit 1.x版本没有直接取消正在进行中任务的方法的,在2.x的版本中,Service的模式变成Call的形式的原因是为了让正在进行的事务可以被取消。
    要做到这点,只需要调用call.cancel()。
    以上就是Retrofit2.0的基本用法了,下面讲讲它的详细用法:

    4.项目中的retrofit的使用情况(基本流程 和 文件和图片上传记录)?

    4.1项目中retrofit的使用流程:
    public interface CommonApi {
    @POST("common/getTime.htm")
    Observable<BaseJson> getNetTime(@Body RequestBody requestBody);
    }

    public class UsualConfigModel extends BaseModel implements UsualConfigContract.Model {
        @Override
        public Observable<BaseJson<Long>> getNetTime(Map<String, Object> hashMap) {
            Observable<BaseJson<Long>> observable = mRepositoryManager.obtainRetrofitService(CommonApi.class)
                    .getNetTime(ToRequestBodyUtil.toRequestBody(hashMap));
            return observable;
        }
    }
    

    public static RequestBody toRequestBody(Map<String, Object> map) {
    String input = buildParams(map);
    input = replaceURL(input);
    //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json, text/plain; charset=utf-8"), input);
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), input);
    return requestBody;
    }

    public static RequestBody buildParamV2(Map<String, String> paramsMap, boolean isFilter) {
    FormBody.Builder builder = new FormBody.Builder();
    for (String key : paramsMap.keySet()) {
    String value = paramsMap.get(key);
    builder.add(key, value);// 追加表单信息
    }
    RequestBody formBody = builder.build();
    //Request request = new Request.Builder().url(netUrl).post(formBody).build();
    return formBody;
    }

    4.2. 项目中retrofit的文件(和图片)上传使用流程:
    VisitApi的类:
    /**
    * @des: 会议详情--新增 :files 文件,图片上传等 : ("", param, files).then( ());
    // 采用这种方式:
    */
    @POST("record/meeting/saveRecordMeetingInfo.htm")
    Observable newAddMeetDetail(@Body RequestBody body);

     @Multipart
    @POST("record/meeting/saveRecordMeetingInfo.htm")
    Observable<BaseCode> newAddMeetDetailV1(@QueryMap Map<String, Object> usermaps, @Part List<MultipartBody.Part>fileMap,
                                            @Part List<MultipartBody.Part>photoMap, @Part List<MultipartBody.Part> signMap);
    
    @Multipart
    @POST("record/meeting/saveRecordMeetingInfo.htm")
    Observable<BaseCode> newAddMeetDetailV2(@QueryMap Map<String, Object> usermaps,    @PartMap Map<String, RequestBody>fileMap,
                                            @PartMap Map<String, RequestBody>photoMap, @PartMap Map<String, RequestBody> signMap);
    

    /**
    * @des: 会议修改的接口
    /
    @Override
    public Observable changedMeetDetail(Map<String, Object> hashMap, List filePaths, List photoPaths, List signPaths) {
    MultipartBody.Builder build = new MultipartBody.Builder();
    for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    build.addFormDataPart(key, String.valueOf(value));//
    }
    if (filePaths != null && filePaths.size() > 0) {
    for (int i = 0; i < filePaths.size(); i++) {
    File file = new File(filePaths.get(i));
    RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    build.addFormDataPart("files", file.getName(), fileRQ);//
    }
    }
    if (photoPaths != null && photoPaths.size() > 0) {
    for (int i = 0; i < photoPaths.size(); i++) {
    File file = new File(photoPaths.get(i));
    RequestBody fileRQ = RequestBody.create(MediaType.parse("image/
    "), file);
    build.addFormDataPart("photos", file.getName(), fileRQ);
    }
    }
    MultipartBody body=build.build(); //MultipartBody继承RequestBody
    Observable observable = mRepositoryManager.obtainRetrofitService(VisitApi.class)
    .changedMeetDetail(body);
    return observable;
    }

    // 不适合的方法:
    public Observable changedMeetDetail2(Map<String, Object> hashMap, List filePaths, List photoPaths, List signPaths) {
    List<MultipartBody.Part> parts1=new ArrayList<>();
    if (filePaths != null && filePaths.size() > 0) {
    for (int i = 0; i < filePaths.size(); i++) {
    File file = new File(filePaths.get(i));
    RequestBody fileRQ = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part part = MultipartBody.Part.createFormData("files", file.getName(), fileRQ);
    parts1.add(part);
    }
    }else {
    MultipartBody.Part part = MultipartBody.Part.createFormData("","");//直接传入两个空字符串就可以了...不能传null;
    parts1.add(part);
    }

        List<MultipartBody.Part> parts2=new ArrayList<>();
        if (photoPaths != null && photoPaths.size() > 0) {
            for (int i = 0; i < photoPaths.size(); i++) {
                File file = new File(photoPaths.get(i));
                RequestBody fileRQ = RequestBody.create(MediaType.parse("image/*"), file);
                MultipartBody.Part part = MultipartBody.Part.createFormData("photos", file.getName(), fileRQ);
                parts2.add(part);
            }
        }else {
            MultipartBody.Part part = MultipartBody.Part.createFormData("","");//直接传入两个空字符串就可以了...不能传null;
            parts2.add(part);
        }
        Observable<BaseCode> observable = null;
    

    // Observable observable= mRepositoryManager.obtainRetrofitService(VisitApi.class)
    // .changedMeetDetail(hashMap, parts1, parts2, parts3);
    return observable;
    }

  • 相关阅读:
    HDU 3853 LOOPS:期望dp【网格型】
    SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
    BZOJ 1629 [Usaco2005 Nov]Cow Acrobats:贪心【局部证明】
    BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
    BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】
    codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
    poj1964最大子矩阵 (单调栈加枚举)
    poj3111 选取物品(二分+贪心)
    codeforces-777E Hanoi Factory (栈+贪心)
    poj3040 发工资(贪心)
  • 原文地址:https://www.cnblogs.com/awkflf11/p/12541197.html
Copyright © 2020-2023  润新知