├── README.md ├── FastJsonRequestBodyConvert.java ├── FastJsonConvertFactory.java └── FastJsonResponseBodyConverter.java /README.md: -------------------------------------------------------------------------------- 1 | # retrofit2_fastjson 2 | use for retrofit 2.0 converterFactory 3 | 4 | ### Retrofit github: [retrofit](https://github.com/square/retrofit) 5 | 6 | 7 | 8 | 9 | 关于Retrofit2.0 版本的介绍看这里: [Retrofit 2.0:有史以来最大的改进](http://gold.xitu.io/entry/56275fc400b08a6c4212da86) 10 | 11 | 12 | 不熟悉Retrofit的童鞋可以看这里: [Retrofit-Java(Android)](http://www.tuicool.com/articles/NnuIva) 13 | -------------------------------------------------------------------------------- /FastJsonRequestBodyConvert.java: -------------------------------------------------------------------------------- 1 | import com.alibaba.fastjson.JSON; 2 | import com.squareup.okhttp.MediaType; 3 | import com.squareup.okhttp.RequestBody; 4 | 5 | import java.io.IOException; 6 | import java.nio.charset.Charset; 7 | 8 | import retrofit.Converter; 9 | 10 | public class FastJsonRequestBodyConvert implements Converter { 11 | 12 | private static final Charset UTF_8 = Charset.forName("UTF-8"); 13 | private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8"); 14 | 15 | @Override 16 | public RequestBody convert(T value) throws IOException { 17 | return RequestBody.create(MEDIA_TYPE,JSON.toJSONString(value).getBytes(UTF_8)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /FastJsonConvertFactory.java: -------------------------------------------------------------------------------- 1 | 2 | import com.squareup.okhttp.RequestBody; 3 | import com.squareup.okhttp.ResponseBody; 4 | 5 | import java.lang.annotation.Annotation; 6 | import java.lang.reflect.Type; 7 | 8 | import retrofit.Converter; 9 | 10 | 11 | public class FastJsonConvertFactory extends Converter.Factory{ 12 | 13 | public static FastJsonConvertFactory create() { 14 | return new FastJsonConvertFactory(); 15 | } 16 | 17 | @Override 18 | public Converter fromResponseBody(Type type, Annotation[] annotations) { 19 | return new FastJsonResponseBodyConverter<>(type); 20 | } 21 | 22 | @Override 23 | public Converter toRequestBody(Type type, Annotation[] annotations) { 24 | return new FastJsonRequestBodyConvert<>(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /FastJsonResponseBodyConverter.java: -------------------------------------------------------------------------------- 1 | import com.alibaba.fastjson.JSON; 2 | import com.squareup.okhttp.ResponseBody; 3 | 4 | import java.io.BufferedReader; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.lang.reflect.Type; 8 | import java.nio.charset.Charset; 9 | 10 | import retrofit.Converter; 11 | 12 | public class FastJsonResponseBodyConverter implements Converter { 13 | 14 | private static final Charset UTF_8 = Charset.forName("UTF-8"); 15 | 16 | private Type type; 17 | public FastJsonResponseBodyConverter(Type type) { 18 | this.type = type; 19 | } 20 | 21 | @Override 22 | public T convert(ResponseBody value) throws IOException { 23 | InputStreamReader isr = null; 24 | BufferedReader bf = null; 25 | try { 26 | isr = new InputStreamReader(value.byteStream(), UTF_8); 27 | bf = new BufferedReader(isr); 28 | StringBuilder buffer = new StringBuilder(); 29 | String line ; 30 | while ((line = bf.readLine()) != null) { 31 | buffer.append(line); 32 | } 33 | return JSON.parseObject(buffer.toString(), type); 34 | } catch (Exception e) { 35 | e.printStackTrace(); 36 | } finally { 37 | if (isr != null) { 38 | try { 39 | isr.close(); 40 | } catch (IOException ignored) { 41 | } 42 | } 43 | if (bf != null) { 44 | try { 45 | bf.close(); 46 | } catch (IOException ignored) { 47 | } 48 | } 49 | } 50 | return null; 51 | } 52 | } 53 | --------------------------------------------------------------------------------