模仿GsonConverter 写的StringConverter 解析String字符串

使用自己写的StringConverter 来封装的 Converter 来解析String private static final RestAdapter CAMERA_CLIENT_NETWORK_SERVICE_REST_ADAPTER_APX = new RestAdapter.Builder()        .setEndpoint("http://192.168.0.1")        .setConverter(new StringConverter())        .setClient(new OkClient())        .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)        .build();

package com.mysnapcam.mscsecure.util;

import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.lang.reflect.Type;

import retrofit.converter.ConversionException;import retrofit.converter.Converter;import retrofit.mime.MimeUtil;import retrofit.mime.TypedInput;import retrofit.mime.TypedOutput;

/** * Created by Administrator on 2016/5/17. * Imitation GsonConverter write StringConverter because when you add Apx returns the String value of the data I wish HTTP response was correct */public class StringConverter implements Converter {    private String charset;

    public StringConverter() {        this("UTF-8");    }

    public StringConverter(String charset) {        this.charset = charset;    }

    @Override    public Object fromBody(TypedInput body, Type type) throws ConversionException {        String charset = this.charset;        if (body.mimeType() != null) {            charset = MimeUtil.parseCharset(body.mimeType(), charset);        }        InputStreamReader isr = null;        try {            isr = new InputStreamReader(body.in(), charset);            StringBuffer sb = new StringBuffer();            char[] b = new char[1024];            int i = -1;            while ((i = isr.read(b)) > -1) {                sb.append(b,0,i);            }

            return sb.toString();        } catch (IOException e) {            throw new ConversionException(e);        } finally {            if (isr != null) {                try {                    isr.close();                } catch (IOException ignored) {                }            }        }

    }

    @Override    public TypedOutput toBody(Object object) {        try {            return new StringTypedOutput(((String) object).getBytes(charset), charset);        } catch (UnsupportedEncodingException e) {            throw new AssertionError(e);        }    }

    private static class StringTypedOutput implements TypedOutput {        private final byte[] stringBytes;        private final String mimeType;

        StringTypedOutput(byte[] StringBytes, String encode) {            this.stringBytes = StringBytes;            this.mimeType = "application/json; charset=" + encode;        }

        @Override        public String fileName() {            return null;        }

        @Override        public String mimeType() {            return mimeType;        }

        @Override        public long length() {            return stringBytes.length;        }

        @Override        public void writeTo(OutputStream out) throws IOException {            out.write(stringBytes);        }    }}
上一篇:dubbo框架----探索-大型系统架构设计(图解)


下一篇:[Everyday Mathematics]20150116