• Retrofit 传递json 和 复杂参数类型List<T>


    1 首先你要定义一个接口

      @POST
      Call<String> post(@Url String url, @Body String info);

    2 创建一个service

            public static RetrofitHttpService createRetrofitHttpService() {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL + "/")
                        .addConverterFactory(StringConverterFactory.create())
                        .build();
    
                RetrofitHttpService Service =
                        retrofit.create(RetrofitHttpService.class);
                return Service;
            }

    3 需要添加一个string转换器

    package org.droidplanner.lelefly.retrofit;
    
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;
    
    import okhttp3.MediaType;
    import okhttp3.RequestBody;
    import okhttp3.ResponseBody;
    import okio.Buffer;
    import retrofit2.Converter;
    import retrofit2.Retrofit;
    
    public class StringConverterFactory extends Converter.Factory {
        private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
        public static StringConverterFactory create() {
            return new StringConverterFactory();
        }
    
        private StringConverterFactory() {
    
        }
    
        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                                Retrofit retrofit) {
            return new Converter<ResponseBody, String>(){
                @Override
                public String convert(ResponseBody value) throws IOException {
                    return value.toString();
                }
            };
        }
    
        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                              Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
            return new Converter<String, RequestBody>(){
                @Override
                public RequestBody convert(String value) throws IOException {
                    Buffer buffer = new Buffer();
                    Writer writer = new OutputStreamWriter(buffer.outputStream(), "utf-8");
                    writer.write(value);
                    writer.close();
                    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
                }
            };
        }
    }

    4 就可以传递jsonarray了,我下面的实例代码传递的是jsonarray

            HttpUtil.post(Lconstant.base + Lconstant.ADD_UAV_MISSION,"[" + 
            resultStr +"]", new ResultCallBack() {
                @Override
                public void onSuccess(String url, String model) {
                    Log.i("===>>",model);
                    showToast("上传服务器成功");
                 
                }
    
                @Override
                public void onFailure(int statusCode, String errorMsg) {
                    showToast(errorMsg);
                }
            });
  • 相关阅读:
    http和https
    openstack
    openstack安全问题
    openstack优势
    java多线程实现方式
    python多进程实现的几种方式
    Java 在提取url 生成图片以及正则表达式
    idea 生成 可执行文件
    dw cs6 支持高分辨率
    svchost.exe 大量占用的问题
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7685146.html
Copyright © 2020-2023  润新知