三默网为您带来有关“java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 19 path $.dat”的文章内容,供您阅读参考。
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 19 path $.dat
2023-01-21 19:48:36
使用Retrofit自带的Gson 连用报以下错误,原因就是bean对象不规范 返回数据类型 不对 但是我用的是 Gsonformat 工具
然后又写了简单的json 进行请求 还是报以下错
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 19 path $.dat
解决问题 直接换fastJson
1创建 FastConverterFactory把下面代码粘过去
使用Retrofit build = new Retrofit.Builder().baseUrl(Utils.Url).addConverterFactory(new FastConverterFactory()).build();
package com.example.retrfit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
/**
* @author
*/
public class FastConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];
private ParserConfig parserConfig = ParserConfig.getGlobalInstance();
private int featureValues = JSON.DEFAULT_PARSER_FEATURE;
private Feature[] features;
private SerializeConfig serializeConfig;
private SerializerFeature[] serializerFeatures;
public FastConverterFactory() {
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, //
Annotation[] annotations, //
Retrofit retrofit) {
return new ResponseBodyConverter<ResponseBody>(type);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, //
Annotation[] parameterAnnotations, //
Annotation[] methodAnnotations, //
Retrofit retrofit) {
return new RequestBodyConverter<RequestBody>();
}
public ParserConfig getParserConfig() {
return parserConfig;
}
public FastConverterFactory setParserConfig(ParserConfig config) {
this.parserConfig = config;
return this;
}
public int getParserFeatureValues() {
return featureValues;
}
public FastConverterFactory setParserFeatureValues(int featureValues) {
this.featureValues = featureValues;
return this;
}
public Feature[] getParserFeatures() {
return features;
}
public FastConverterFactory setParserFeatures(Feature[] features) {
this.features = features;
return this;
}
public SerializeConfig getSerializeConfig() {
return serializeConfig;
}
public FastConverterFactory setSerializeConfig(SerializeConfig serializeConfig) {
this.serializeConfig = serializeConfig;
return this;
}
public SerializerFeature[] getSerializerFeatures() {
return serializerFeatures;
}
public FastConverterFactory setSerializerFeatures(SerializerFeature[] features) {
this.serializerFeatures = features;
return this;
}
final class ResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private Type type;
ResponseBodyConverter(Type type) {
this.type = type;
}
public T convert(ResponseBody value) throws IOException {
try {
return JSON.parseObject(value.string()
, type
, parserConfig
, featureValues
, features != null
? features
: EMPTY_SERIALIZER_FEATURES
);
} finally {
value.close();
}
}
}
final class RequestBodyConverter<T> implements Converter<T, RequestBody> {
RequestBodyConverter() {
}
public RequestBody convert(T value) throws IOException {
byte[] content = JSON.toJSONBytes(value
, serializeConfig == null
? SerializeConfig.globalInstance
: serializeConfig
, serializerFeatures == null
? SerializerFeature.EMPTY
: serializerFeatures
);
return RequestBody.create(MEDIA_TYPE, content);
}
}
}
推荐使用
https://github.com/ligboy/retrofit-converter-fastjson
打开这个github地址,目前最新的版本是
compile 'org.ligboy.retrofit2:converter-fastjson-android:2.1.0'
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(FastJsonConverterFactory.create())//重点是这句话
.build();