使用自己写的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);        }    }}

模仿GsonConverter 写的StringConverter 解析String字符串的更多相关文章

  1. JAVA代码解析String字符串(json格式的)

    java解析String字符串(json格式) 需要jar包:json-lib-2.4-jdk15.jar 一. String str = "{\"name\":\&qu ...

  2. java基础18 String字符串和Object类(以及“equals” 和 “==”的解析)

    一.String字符串 问:笔试题:new String("abc")创建了几个对象?答:两个对象,一个对象是 位于堆内存,一个对象位于字符串常量池 class Demo17 { ...

  3. 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。

    服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...

  4. 一个用C++写的Json解析与处理库

    什么是Json?这个库能做什么? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is e ...

  5. Json转model对象,model转json,解析json字符串

    GitHub链接: https://github.com/mozhenhau/D3Json D3Json 通过swift的反射特性,把json数据转换为model对象,本类最主要是解决了其他一般jso ...

  6. java 写一个JSON解析的工具类

    上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...

  7. 一、JSON解析与字符串化

    JSON.stringify() 序列化对象.数组或原始值 语法:JSON.stringify(o,filter,indent) o,要转换成JSON的对象.数组或原始值 filter,指定要序列化的 ...

  8. 深入解析String#intern

    转自:https://tech.meituan.com/in_depth_understanding_string_intern.html 深入解析String#intern john_yang ·2 ...

  9. JS的全局函数eval解析JSON字符串

    JavaScript eval() 函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需. ...

随机推荐

  1. Spring Cloud App(Service) Pom示例

    都配对了才能找到jar包(无法访问外网时是如何配的?) parent dependencyManageMent repositories plugInRepositories <groupId& ...

  2. PHP保存base64

    base64图片格式:$base64_url = data:image/jpeg;base64,xxxxxxxxxxxxxxxxxxxxxx 1,去除头部:$base64_body = substr( ...

  3. php操作memcache的用法、详解和方法介绍

    1.简介 memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问. memcache也提供用于通信对话(sess ...

  4. mongodb数据库操作--备份 还原 导出 导入

    首先数据库备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径  mongodump -h 127.0.0.1 -u admin -p ...

  5. 一个小程序[Socrates]中学到的Perl点滴

    1. 抓取网页源文件,只要三行代码 use LWP::Simple; $url='http://music.baidu.com/top/dayhot'; $page=get($url) or die ...

  6. POJ 3258 River Hopscotch 二分枚举

    题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...

  7. BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛

    Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...

  8. 如何把关联性的告警智能添加到 Nagios 上?(2)

    上节回顾 对于许多 IT 和运维团队来说,Nagios 既是一个福音也是一个诅咒.一方面,Naigos 在 IT 应用的工作领域中,给予了你可以实时查看告警数据的可能性:但是另一方面,Nagios 也 ...

  9. NetFlow网络流量监测技术的应用和设计(转载)

    http://blog.chinaunix.net/uid-20466300-id-1672909.html http://www.cww.net.cn/news/html/2014/12/25/20 ...

  10. Analysis Guidelines

    This section describes some best practices for analysis. These practices come from experience of ana ...