相关Question
Json
的回复是New line delimiter Json
:
{"type":"data","id":"xyz"}
{"type":"value","id":"xcf"}
....
....
我正在使用Retrofit
发出请求:
public void getWarehouse(){
//Generation of RestAdapter
RestAdapter adapter = new RestAdapter.Builder ()
.setEndpoint(URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(new AndroidLog("= NETWORK ="))
.build();
//Making request to API
adapter.create(WarehouseAPI.class).getWarehouse()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Response>() {
@Override
public void onCompleted() {
Log.d(this.getClass().getName(), "OnCompleted ()");
}
@Override
public void onError(Throwable e) {
Log.d(this.getClass().getName(), "Error:" + e.toString());
}
@Override
public void onNext(Response response) {
System.out.println("test");
}
});
}
我可以在Android Studio console
中看到回复,但会收到以下错误:
Error:retrofit.RetrofitError: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $
NdJson
,且无法正确解析答案 0 :(得分:0)
正如@ trevor-e所提到的,你必须实现一个自定义转换器来处理ndjson格式。请查看以下链接以获取入门指南:
Retrofit — Define a Custom Response Converter
还可以从How can I return String or JSONObject from asynchronous callback using Retrofit?
查看StringConverter您可以将响应转换为常规字符串,然后将每行解析为JSON对象并从那里继续。