简体   繁体   English

在Android改造响应中获取JsonSyntaxException?

[英]Getting JsonSyntaxException in Android retrofit response?

Related Question 相关问题

Response of Json is New line delimiter Json : Json响应是New line delimiter Json

{"type":"data","id":"xyz"}
{"type":"value","id":"xcf"}
....
....

I am using Retrofit to make request: 我正在使用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");

         }


       });

}

I can see the response in my Android Studio console but getting following error: 我可以在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 $

Possibility of Error 错误的可能性

  • Response is in NdJson and it's not able to parse it correctly 响应位于NdJson ,无法正确解析

Question

  • How can I parse it correctly? 如何正确解析?

As @trevor-e mentions, you have to implement a custom converter to handle the ndjson format. 正如@ trevor-e所提到的,您必须实现一个自定义转换器来处理ndjson格式。 Check link below for a guide to start: 查看以下链接以获取入门指南:

Retrofit — Define a Custom Response Converter 改造—定义自定义响应转换器

Also check out the StringConverter from: How can I return String or JSONObject from asynchronous callback using Retrofit? 还可以从以下位置检查StringConverter: 如何使用Retrofit从异步回调返回String或JSONObject?

You could potentially convert the response to a regular string then parse each line into a JSON object and proceed from there. 您可以将响应转换为常规字符串,然后将每一行解析为一个JSON对象,然后从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM