zoukankan      html  css  js  c++  java
  • 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);
                }
            });
  • 相关阅读:
    Asp.NET调用有道翻译API
    JSON C# Class Generator ---由json字符串生成C#实体类的工具
    让jQuery的contains方法不区分大小写
    javascript parseUrl函数(来自国外的获取网址url参数)
    typescript
    webpack 第二部分
    express node 框架介绍
    webpack 最新版
    es6 字符串 对象 拓展 及 less 的语法
    es6 的数组的方法
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7685146.html
Copyright © 2011-2022 走看看