您好,以下代码没有从json获得任何成功响应。我正在将数据发送到服务器。基于请求要获取响应。 但是,到目前为止,请求仍在进行中,而未从服务器获得任何响应。
调试代码也没有错误,但是没有从服务器获得任何响应。
任何人都可以帮助我
json请求:
{"Status":"1",
"lightID":"1234",
"lightType":"1",
"level":"4"
}
json响应:
{
"status": [
{
"id": "1234",
"status": 1,
"val": null
}
]
}
OnOff模型类:
public class OnOff {
@SerializedName("id")
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
@SerializedName ("status")
private Integer status;
@SerializedName ("val")
private String val;
}
Onclicklistner:
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) {
if (response!=null) {
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 ( );
}
}
}
});
OnOffStatusList模型类:
public class OnOffStatusList {
@SerializedName ("status")
@Expose
private ArrayList<OnOff> status=null;
public ArrayList <OnOff> getStatus() {
return status;
}
public void setStatus(ArrayList <OnOff> status) {
this.status = status;
}
}
API.java
@Headers("Content-Type: application/json")
@POST("OnOff")
Call<OnOffStatusList> getMyJSON(@Body String body);