1

Related Question

Response of Json is New line delimiter Json :

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

I am using Retrofit to make request:

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:

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

Question

  • How can I parse it correctly?
Community
  • 1
  • 1
Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • Are you trying to read it as an array? Should your response be wrapped in []? – telkins Oct 19 '15 at 07:15
  • @trevor-e No actually it's `NdJson`. So No `JsonArray` or `JsonObject` will work here . Please have a look at this question:http://stackoverflow.com/questions/15121584/how-to-parse-a-large-newline-delimited-json-file-by-jsonstream-module-in-node-j (If I am not wrong) – Amit Pal Oct 19 '15 at 07:18
  • I see. I believe you will need to specify a customer converter to retrofit to handle that, possibly using a custom Gson object. – telkins Oct 19 '15 at 07:22
  • I don't have enough experience in `retrofit` but is it possible to get response in any form. I mean can I get the response as it is from the request through above code (because I can see the data coming through retrofit in my studio console) – Amit Pal Oct 19 '15 at 07:24

1 Answers1

0

As @trevor-e mentions, you have to implement a custom converter to handle the ndjson format. 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?

You could potentially convert the response to a regular string then parse each line into a JSON object and proceed from there.

Community
  • 1
  • 1
keno
  • 2,956
  • 26
  • 39