嗨,在下面的代码中,我有一个名为mPreset的按钮。如果单击该按钮将get方法发送到服务器并尝试获取响应。如果响应成功,则打印json数据。 / p>
但是我的light_id没有打印。谁能帮助解决这个问题
在下面的代码中,我有一个名为GetScheduler的类,其中一个是String类型,其余两个是整数类型数组。
GetScheduler.java:
public class GetScheduler {
@SerializedName("status")
private String status;
@SerializedName("data")
private DataClass data;
public DataClass getData() {
return data;
}
public void setData(DataClass data) {
this.data= data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public class DataClass {
@SerializedName("light_id")
private String light_id;
@SerializedName("intensity")
private int[] intensity;
@SerializedName("cct")
private int[] cct;
public String getLight_id() {
return light_id;
}
public void setLight_id(String light_id) {
this.light_id = light_id;
}
public int[] getIntensity() {
return intensity;
}
public void setIntensity(int[] intensity) {
this.intensity = intensity;
}
public int[] getCct() {
return cct;
}
public void setCct(int[] cct) {
this.cct = cct;
}
}
在下面的类中描述了名为API的接口
API.java:
public interface API {
@retrofit2.http.GET("/gateway_schedule")
retrofit2.Call<GetScheduler> getSchedulerData();
}
Main.java:
mPreset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getCCTAndIntensityValuesForPreset();
}
});
private void getCCTAndIntensityValuesForPreset() {
String url = "http://172.24.1.1:9000";
Retrofit retrofit = null;
Log.d("123", "retrofit");
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
Log.d("123", "build();");
}
API service = retrofit.create(API.class);
Call<GetScheduler> call=service.getSchedulerData();
Log.d("123", "Call<List<GetScheduler>> call = service.getSchedulerData();");
call.enqueue(new Callback<GetScheduler>() {
@Override
public void onResponse(Call<GetScheduler> call, Response<GetScheduler> response) {
if(response!=null&&response.isSuccessful()){
String getLightId=response.body().getData().getLight_id();
Toast.makeText(getApplicationContext(),"Light Id"+getLightId,Toast.LENGTH_LONG).show();
//String light_id=response.body()
int[] getIntensty=response.body().getData().getIntensity();
for(int i=0;i<getIntensty.length;i++){
getIntensty[i]=i;
}
int[] getCCT=response.body().getData().getCct();
for(int i=0;i<getCCT.length;i++){
getCCT[i]=i;
mCCT1.setProgress(getCCT[0]);
mCCT2.setProgress(getCCT[1]);
mCCT3.setProgress(getCCT[2]);
mCCT4.setProgress(getCCT[3]);
mCCT5.setProgress(getCCT[4]);
mCCT6.setProgress(getCCT[5]);
mCCT7.setProgress(getCCT[6]);
mCCT8.setProgress(getCCT[7]);
mCCT9.setProgress(getCCT[8]);
mCCT10.setProgress(getCCT[9]);
mCCT11.setProgress(getCCT[10]);
mCCT12.setProgress(getCCT[11]);
}
}
};
@Override
public void onFailure(Call<GetScheduler> call, Throwable t) {
t.printStackTrace();
}
});
}
Json:
{"status":"success","data":{"light_id":"00","intensity":[60,60,60,60,60,60,60,60,60,60,60,60],"cct":[100,100,100,100,90,90,80,90,78,89,89,90]}}
答案 0 :(得分:0)
这样更改您的回复
public class GetScheduler {
@SerializedName("status")
private String status;
@SerializedName("data")
private DataClass data;
public DataClass getData() {
return data;
}
public void setData(DataClass data) {
this.data= data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public class DataClass {
@SerializedName("light_id")
private String light_id;
@SerializedName("intensity")
private int[] intensity;
@SerializedName("cct")
private int[] cct;
public String getLight_id() {
return light_id;
}
public void setLight_id(String light_id) {
this.light_id = light_id;
}
public int[] getIntensity() {
return intensity;
}
public void setIntensity(int[] intensity) {
this.intensity = intensity;
}
public int[] getCct() {
return cct;
}
public void setCct(int[] cct) {
this.cct = cct;
}
}
}
如果您有数组响应(如我所见),则必须使用List<GetScheduler>
,否则,您只能使用GetScheduler
所以全部更改
List<GetScheduler>
收件人
GetScheduler
API.java
public interface API {
@retrofit2.http.GET("/gateway_schedule")
retrofit2.Call<GetScheduler> getSchedulerData();
}
还有Main.java
private void getCCTAndIntensityValuesForPreset() {
try {
String url = "http://172.24.1.1:9000";
Retrofit retrofit = null;
Log.d("123", "retrofit");
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
Log.d("123", "build();");
}
API service = retrofit.create(API.class);
Call<GetScheduler> call=service.getSchedulerData();
Log.d("123", "Call<GetScheduler> call = service.getSchedulerData();");
call.enqueue(new Callback<GetScheduler>() {
@Override
public void onResponse(Call<GetScheduler> call, Response<GetScheduler> response) {
if(response!=null&&response.isSuccessful()){
String getLightId=response.body().getData().getLight_id().toString();
Toast.makeText(getApplicationContext(),"Light Id"+getLightId,Toast.LENGTH_LONG).show();
//String light_id=response.body()
int[] getIntensty=response.body().getData().getIntensity();
for(int i=0;i<getIntensty.length;i++){
getIntensty[i]=i;
}
int[] getCCT=response.body().getData().getCct();
for(int i=0;i<getCCT.length;i++){
getCCT[i]=i;
mCCT1.setProgress(getCCT[0]);
mCCT2.setProgress(getCCT[1]);
}
}
};
@Override
public void onFailure(Call<GetScheduler> call, Throwable t) {
t.printStackTrace();
}
});
}catch (Exception e) {
e.printStackTrace();
}
}
您必须像这样在onResponse
中更改此行
String getLightId=response.body().getData().getLight_id().toString();
我建议您在onFailure()中添加日志并查看logCat并删除try catch
@Override
public void onFailure(Call<List<GetScheduler>> call, Throwable t) {
t.printStackTrace();
}
答案 1 :(得分:0)
您需要创建2个类似的类
package com.your.package;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Data {
@SerializedName("light_id")
@Expose
private String lightId;
@SerializedName("intensity")
@Expose
private List<Integer> intensity = null;
@SerializedName("cct")
@Expose
private List<Integer> cct = null;
public String getLightId() {
return lightId;
}
public void setLightId(String lightId) {
this.lightId = lightId;
}
public List<Integer> getIntensity() {
return intensity;
}
public void setIntensity(List<Integer> intensity) {
this.intensity = intensity;
}
public List<Integer> getCct() {
return cct;
}
public void setCct(List<Integer> cct) {
this.cct = cct;
}
}
-----------------------------------com.your.package.GetScheduler.java-----------------------------------
package com.your.package;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class GetScheduler {
@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private Data data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}