简体   繁体   English

使用改造无法从android中的json响应中获取light_id

[英]Not getting light_id from json response in android using retrofit

Hi in the below code i have a button named as mPreset .If am click on that am sending get method to the server and am trying to get the response.if the response is is successful then am printing the json data. 您好在下面的代码中,我有一个名为mPreset的按钮。如果单击该按钮将get方法发送到服务器并尝试获取响应。如果响应成功,则打印json数据。

But my light_id is not printing. 但是我的light_id没有打印。 can any one please help to resolve this issues 谁能帮忙解决这个问题

In the below code i have a class named as GetScheduler in that one is String type and remaining two are array of integer types. 在下面的代码中,我有一个名为GetScheduler的类,其中一个是String类型,其余两个是整数类型数组。

GetScheduler.java: 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;
        }
    }

In the below class describes the interface named as API 在下面的类中描述名为API的接口

API.java: API.java:

 public interface API {
    @retrofit2.http.GET("/gateway_schedule")
    retrofit2.Call<GetScheduler> getSchedulerData();

    }

Main.java: 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: 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]}}

change your response like this 像这样改变你的回应

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

If you have array response (as I see you don't) you must use List<GetScheduler> else you must use only GetScheduler 如果您有数组响应(如我所见),则必须使用List<GetScheduler>否则必须仅使用GetScheduler

So Change all 所以改变所有

 List<GetScheduler>

To

 GetScheduler

API.java

public interface API {
@retrofit2.http.GET("/gateway_schedule")
     retrofit2.Call<GetScheduler> getSchedulerData();
}

And Main.java 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();
    }

}

And you must change this line in onResponse like this 而且您必须像这样在onResponse更改此行

String getLightId=response.body().getData().getLight_id().toString();

I suggest you to add log in onFailure() and see logCat and remove try catch 我建议您在onFailure()中添加日志并查看logCat并删除try catch

@Override
public void onFailure(Call<List<GetScheduler>> call, Throwable t) {
     t.printStackTrace();
}

You need to Create 2 Class like 您需要创建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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM