android - retrofit中gson解析
PHP中文网
PHP中文网 2017-04-18 09:15:00
0
1
447

retrofit中gsonconverter 对于空集合 array:[] 这种解析会报错 请问有什么解决办法吗

PHP中文网
PHP中文网

认证高级PHP讲师

Antworte allen(1)
小葫芦

自定义类继承Converter.Factory,改写convert()方法如下:

@Override
public T convert(ResponseBody value) throws IOException {
    String response = value.string();

    try {
        return gson.fromJson(response, type);
    } catch (JsonSyntaxException e) {
        try {
            org.json.JSONObject json = new org.json.JSONObject(response);
            String data = json.getString("data");

            if (TextUtils.isEmpty(data) || data.equals("[]")) {
                // 添加对空Json数组的处理逻辑
            }
        } catch (JSONException e1) {
            // no op
        }

        throw new ApiException(ApiException.JSON_PARSE_ERROR, ApiException.JSON_PARSE_ERROR_MESSAGE);
    } finally {
        value.close();
    }
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!