0

Image

Hi in the below error is coming when am trying to parse the JSON object as a string.

When Am sending JSON object to server

Json request:

{"name":"Building 1","level":1}

Json Response:

{"status":"success"}

Below one is edited code with the response. But, I am not getting any response from the server.

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(interceptor).build();

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(API.URL_BASE)
    .client(client)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

API service = retrofit.create(API.class);
try {
    JSONObject parmobject=new JSONObject ();
    parmobject.put("name",name);
    parmobject.put("level",level);
    Call<NewBuilding> userCall = service.getbuildinglist (parmobject.toString ());
    userCall.enqueue(new Callback<NewBuilding> () {
        @Override
        public void onResponse(Call<NewBuilding> call, Response<NewBuilding> response) {
            if (response != null && response.code ()==200) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject (String.valueOf (response));
                } catch (JSONException e) {
                    Log.e("My App", "Could not parse malformed JSON:");
                }

                String status = response.body ( ).getStatus ( ).toString ( );

                if(status.equalsIgnoreCase ("success")){
                    makeText(getActivity (), "Building successfully Added", Toast.LENGTH_SHORT).show();
                    arrayList.add (name);
                    adapter.notifyDataSetChanged ();
                }
            } else {
                makeText(getActivity (), "Please try again for Adding New Building", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<NewBuilding> call, Throwable t) {
            t.printStackTrace ();
        }
    });
} catch (JSONException e) {
    e.printStackTrace();
}
Hasitha Jayawardana
  • 2,152
  • 3
  • 14
  • 32

1 Answers1

0

You can add interceptor for what responses you are getting.

build.gradle

implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'

In retrofit

if (postService == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder()
                    .addInterceptor(interceptor).build();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(base_url)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            postService = retrofit.create(PostService.class);

        }
        return postService;

Then you can see in verbose in log all the responses will be seen.

in response

      @Override
      public void onResponse(Call<NewBuilding> call, Response <NewBuilding> response) {
      try {

        JSONObject obj = new JSONObject(response);
        Integer response1= obj.code();



      } catch (Throwable tx) {
        Log.e("My App", "Could not parse malformed JSON: \"" + json + "\"");
      }
      }

Logcat:

W/System.err: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ W/System.err: at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1568) at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1409) at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:593) at com.google.gson.stream.JsonReader.peek(JsonReader.java:425) W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:205) at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39) W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223) at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121) at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206) W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)

Ramesh R
  • 6,273
  • 2
  • 21
  • 31
Rajkumar Sharma
  • 479
  • 1
  • 3
  • 9