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();
}