Hi in the below code am getting null pointer exception from server.csn any please check it out and let me know where i did the mistake.
Not executing the particular if condition can any one check it my updated code
json response:
{
"status": [
{
"id": "1234",
"status": 1,
"val": null
}
]
}
Below code for ONOFF in this we are getting response.body() am getting null. response.code() am getting 200 ok
ONOFF:
OnOFF.setOnToggledListener (new OnToggledListener ( ) {
@Override
public void onSwitched(LabeledSwitch labeledSwitch, boolean isOn) {
String lightID="";
String lightType="";
String level="";
String Status="";
if(isOn) {
Retrofit retrofit = new Retrofit.Builder ( )
.baseUrl (API.URL_BASE)
.addConverterFactory (ScalarsConverterFactory.create ( ))
.addConverterFactory (GsonConverterFactory.create ( )).build ( );
API service = retrofit.create (API.class);
lightID = "1234";
lightType = "1";
level = "4";
try {
if (OnOFF.isOn ( )) {
Status = "1";
} else {
Status = "0";
}
JSONObject parmobject = new JSONObject ( );
parmobject.put ("Status", Status);
parmobject.put ("lightID", lightID);
parmobject.put ("lightType", lightType);
parmobject.put ("level", level);
// luminary.setImageDrawable(ContextCompat.getDrawable(getContext (), R.drawable.luminaryon));
Call<OnOffStatusList> userCall = service.getMyJSON (parmobject.toString ());
userCall.enqueue(new Callback<OnOffStatusList> () {
@Override
public void onResponse(Call<OnOffStatusList> call, Response<OnOffStatusList> response) {
String response1= response.body ().toString ();
Log.d ("response code",response1);
if (response1.equals ("200")) {
onoffStatusList=response.body ().getStatus ();
String id=onoffStatusList.get (0).getId ();
Integer status=onoffStatusList.get (1).getStatus();
String val=onoffStatusList.get (2).getVal ();
if(status==1) {
luminary.setImageDrawable (ContextCompat.getDrawable (getContext ( ), R.drawable.luminaryon));
}
else {
luminary.setImageDrawable (ContextCompat.getDrawable (getContext ( ), R.drawable.luminaryoff));
}
// String status=response.body ().getMatches ().toString ();
}
}
@Override
public void onFailure(Call<OnOffStatusList> call, Throwable t) {
// Toast.makeText(LoginActivity.this, "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// dialog.dismiss();
}
});
} catch (JSONException e) {
e.printStackTrace ( );
}
}
}
});
API:
@Headers("Content-Type: application/json")
@POST("OnOff")
Call<OnOffStatusList> getMyJSON(@Body String body);