美文网首页
Retrofit入门--Json解析

Retrofit入门--Json解析

作者: four_k | 来源:发表于2017-06-12 16:24 被阅读0次

    public classCustomGsonConverterFactoryextendsConverter.Factory {

    private  finalMediaTypeMEDIA_TYPE= MediaType.parse("application/json; charset=UTF-8");

    private  finalCharsetUTF_8= Charset.forName("UTF-8");

    private finalGsongson;

    privateCustomGsonConverterFactory(Gson gson) {

    if(gson ==null)throw newNullPointerException("gson == null");

    this.gson= gson;

    }

    public staticCustomGsonConverterFactorycreate() {

    returncreate(newGson());

    }

    public staticCustomGsonConverterFactorycreate(Gson gson) {

    return newCustomGsonConverterFactory(gson);

    }

    @Override

    publicConverterresponseBodyConverter(Type type,Annotation[] annotations,Retrofit retrofit) {

    TypeAdapter adapter =gson.getAdapter(TypeToken.get(type));

    return newCustomGsonResponseBodyConverter<>(gson,adapter);

    }

    @Override

    publicConverterrequestBodyConverter(Type type,Annotation[] parameterAnnotations,Annotation[] methodAnnotations,Retrofit retrofit) {

    TypeAdapter adapter =gson.getAdapter(TypeToken.get(type));

    return newCustomGsonRequestBodyConverter<>(gson,adapter);

    }

    final classCustomGsonRequestBodyConverterimplementsConverter {

    private finalGsongson;

    private finalTypeAdapteradapter;

    CustomGsonRequestBodyConverter(Gson gson,TypeAdapter adapter) {

    this.gson= gson;

    this.adapter= adapter;

    }

    @Override

    publicRequestBodyconvert(Tvalue)throwsIOException {

    Buffer buffer =newBuffer();

    Writer writer =newOutputStreamWriter(buffer.outputStream(),UTF_8);

    JsonWriter jsonWriter =gson.newJsonWriter(writer);

    adapter.write(jsonWriter,value);

    jsonWriter.close();

    returnRequestBody.create(MEDIA_TYPE,buffer.readByteString());

    }

    }

    final classCustomGsonResponseBodyConverterimplementsConverter {

    private finalGsongson;

    private finalTypeAdapteradapter;

    CustomGsonResponseBodyConverter(Gson gson,TypeAdapter adapter) {

    this.gson= gson;

    this.adapter= adapter;

    }

    @Override

    publicTconvert(ResponseBody value)throwsIOException {

    String response = value.string();

    HttpStatus httpStatus =gson.fromJson(response,HttpStatus.class);

    if(httpStatus.isCodeInvalid()) {

    value.close();

    throw newApiException(httpStatus.getCode(),httpStatus.getMessage());

    }

    MediaType contentType = value.contentType();

    Charset charset = contentType !=null? contentType.charset(UTF_8) :UTF_8;

    InputStream inputStream =newByteArrayInputStream(response.getBytes());

    Reader reader =newInputStreamReader(inputStream,charset);

    JsonReader jsonReader =gson.newJsonReader(reader);

    try{

    returnadapter.read(jsonReader);

    }finally{

    value.close();

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:Retrofit入门--Json解析

          本文链接:https://www.haomeiwen.com/subject/whmlqxtx.html