您好,在下面运行的小型应用程序中包含一个带标签的开关,其中包含开/关。 如果我按下on开关,则正在向服务器发送数据。如果响应成功,则想更改灯的颜色。
如果我按下关闭按钮想要更改灯的颜色。
下面的代码可以请您检查一下,并让我知道我在哪里做错了。
它没有执行此respopnse。我没有从服务器得到任何响应。 一旦响应为200,则它正在重现json数组并希望获取状态。如果状态为1,则要打开灯或关闭灯
Call<OnOff> userCall = service.Onoff(parmobject.toString());
Log.d ("usercall",userCall.toString ());
userCall.enqueue(new Callback<helper.OnOff> () {
@Override
public void onResponse(Call<helper.OnOff> call, Response<helper.OnOff> response) {
ONOFF pojo
import java.util.List;
public class OnOff {
public List <Status> getStatus() {
return status;
}
public void setStatus(List<Status> status) {
this.status = status;
}
@SerializedName("status")
@Expose
private List<Status> status = null;
}
status.java:
public class Status {
@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;
}
Json的回复:
{
"status": [
{
"id": "1234",
"status": 1,
"val": null
}
]
}
ONOFF.java:
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<OnOff> userCall = service.Onoff(parmobject.toString());
userCall.enqueue(new Callback<helper.OnOff> () {
@Override
public void onResponse(Call<helper.OnOff> call, Response<helper.OnOff> response) {
if (response != null && response.isSuccessful ( ) && response.code ( ) == 200) {
luminary.setImageDrawable(ContextCompat.getDrawable(getContext (), R.drawable.luminaryon));
// String status=response.body ().getMatches ().toString ();
}
}
@Override
public void onFailure(Call<helper.OnOff> call, Throwable t) {
// Toast.makeText(LoginActivity.this, "Some error occurred -> ", Toast.LENGTH_LONG).show();;
// dialog.dismiss();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
luminary.setImageDrawable(ContextCompat.getDrawable(getContext (), R.drawable.luminaryoff));
}
}
});