没有使用改造从android中的json响应中获取light_id

在下面的代码中,我有一个名为 mPreset 的按钮。如果单击该按钮,则将 get 方法发送到服务器并尝试获取响应。如果响应成功,则打印 json 数据。


但我的 light_id 没有打印。任何人都可以帮助解决这个问题


在下面的代码中,我有一个名为 GetScheduler 的类,其中一个是字符串类型,其余两个是整数类型数组。


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


哔哔one
浏览 164回答 2
2回答

万千封印

您需要创建 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")@Exposeprivate String lightId;@SerializedName("intensity")@Exposeprivate List<Integer> intensity = null;@SerializedName("cct")@Exposeprivate 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")@Exposeprivate String status;@SerializedName("data")@Exposeprivate 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;}}

当年话下

像这样改变你的反应public class GetScheduler {&nbsp; &nbsp;@SerializedName("status")&nbsp; &nbsp;private String status;&nbsp; &nbsp;@SerializedName("data")&nbsp; &nbsp;private DataClass data;&nbsp; &nbsp;public DataClass getData() {&nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp;}&nbsp; &nbsp;public void setData(DataClass data) {&nbsp; &nbsp; &nbsp; &nbsp;this.data= data;&nbsp; &nbsp;}&nbsp; &nbsp;public String getStatus() {&nbsp; &nbsp; &nbsp; &nbsp;return status;&nbsp; &nbsp;}&nbsp; &nbsp;public void setStatus(String status) {&nbsp; &nbsp; &nbsp; this.status = status;&nbsp; &nbsp; }&nbsp; &nbsp;public class DataClass {&nbsp; &nbsp; &nbsp; &nbsp; @SerializedName("light_id")&nbsp; &nbsp; &nbsp; &nbsp; private String light_id;&nbsp; &nbsp; &nbsp; &nbsp; @SerializedName("intensity")&nbsp; &nbsp; &nbsp; &nbsp; private int[] intensity;&nbsp; &nbsp; &nbsp; &nbsp; @SerializedName("cct")&nbsp; &nbsp; &nbsp; &nbsp; private int[] cct;&nbsp; &nbsp; &nbsp; &nbsp; public String getLight_id() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return light_id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setLight_id(String light_id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.light_id = light_id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int[] getIntensity() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return intensity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setIntensity(int[] intensity) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.intensity = intensity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int[] getCct() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return cct;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setCct(int[] cct) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.cct = cct;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}}如果你有数组响应(我看到你没有)你必须使用List<GetScheduler>否则你必须只使用GetScheduler所以改变所有&nbsp;List<GetScheduler>到&nbsp;GetSchedulerAPI.javapublic interface API {@retrofit2.http.GET("/gateway_schedule")&nbsp; &nbsp; &nbsp;retrofit2.Call<GetScheduler> getSchedulerData();}和Main.javaprivate void getCCTAndIntensityValuesForPreset() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; String url = "http://172.24.1.1:9000";&nbsp; &nbsp; &nbsp; &nbsp; Retrofit retrofit = null;&nbsp; &nbsp; &nbsp; &nbsp; Log.d("123", "retrofit");&nbsp; &nbsp; &nbsp; &nbsp; if (retrofit == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retrofit = new Retrofit.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .baseUrl(url)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConverterFactory(GsonConverterFactory.create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("123", "build();");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; API service = retrofit.create(API.class);&nbsp; &nbsp; &nbsp; &nbsp; Call<GetScheduler> call=service.getSchedulerData();&nbsp; &nbsp; &nbsp; &nbsp; Log.d("123", "Call<GetScheduler> call = service.getSchedulerData();");&nbsp; &nbsp; &nbsp; &nbsp; call.enqueue(new Callback<GetScheduler>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(Call<GetScheduler> call, Response<GetScheduler> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(response!=null&&response.isSuccessful()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String getLightId=response.body().getData().getLight_id().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(),"Light Id"+getLightId,Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //String light_id=response.body()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] getIntensty=response.body().getData().getIntensity();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<getIntensty.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getIntensty[i]=i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] getCCT=response.body().getData().getCct();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<getCCT.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getCCT[i]=i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCCT1.setProgress(getCCT[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCCT2.setProgress(getCCT[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<GetScheduler> call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}你必须onResponse像这样改变这条线String getLightId=response.body().getData().getLight_id().toString();我建议您添加登录 onFailure() 并查看 logCat 并删除 try catch@Overridepublic void onFailure(Call<List<GetScheduler>> call, Throwable t) {&nbsp; &nbsp; &nbsp;t.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java