使用翻新的android中json的空指针异常

时间:2019-06-28 12:16:32

标签: android

嗨,下面的代码正在从server.csn中获取空指针异常,请检查出来,让我知道我在哪里出错了。

如果条件可以让任何人检查我的更新代码,则不执行特定的操作

json响应:

{
    "status": [
        {
            "id": "1234",
            "status": 1,
            "val": null
        }
    ]
}

在下面的ONOFF代码中,我们得到response.body()得到null。 response.code()恢复正常200

打开:

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

1 个答案:

答案 0 :(得分:0)

在response.body()上添加null检查以及响应。 if (response!=null&&response.body()!=null)