简体   繁体   English

响应不是来自json在Android中使用翻新

[英]response not coming from json using retrofit in android

Hi in the below code am sending method as get and getting the response from and want to display that response want to set to variables. 嗨,在下面的代码中,发送方法是get和get的响应,并希望显示要设置为变量的响应。

But am not getting any response from data.am sending get method using that method am getting response .after the response is successful and then want to set that text to textviews 但是没有从data.am获得任何响应。使用该方法发送get方法正在获取响应。响应成功后,然后想要将该文本设置为textviews

API.java: API.java:

public interface API {
    public static final String BASE_URL="ip address";

        @GET("/gateway_schedule")
        Call<List<GetScheduler>> getSchedulerData();

}

Scheduler.java: Scheduler.java:

mPreset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getCCTAndIntensityValuesForPreset();

            }
        });
private void getCCTAndIntensityValuesForPreset() {

    try {
        String url = "http://XXXXXXXXXX/";

        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<List<GetScheduler>> call = (Call<List<GetScheduler>>) service.getSchedulerData();
        Log.d("123", "Call<List<GetScheduler>> call = service.getSchedulerData();");
        call.enqueue(new Callback<List<GetScheduler>>() {
            @Override
            public void onResponse(Call<List<GetScheduler>> call, Response<List<GetScheduler>> response) {
                if(response!=null&&response.isSuccessful()){
                    String getLightId=response.body().get(0).getLight_id().toString();
                    Toast.makeText(getApplicationContext(),"Light Id"+getLightId,Toast.LENGTH_LONG).show();
                    //String light_id=response.body()
                }

            }

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

            }



        });

    }catch (Exception e) {Log.d("123", "Exception");}

} }

Getscheduler.java: Getscheduler.java:

public class GetScheduler {
@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;
}

} }

Add GsonConverter to convert json data to Pojo classes. 添加GsonConverter将json数据转换为Pojo类。

 if (retrofit == null) { 
      retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL) 
                .addConverterFactory(GsonConverterFactory.create()) 
                .build(); 
    }

and add dependency in build.gradle file 并在build.gradle文件中添加依赖项

compile 'com.squareup.retrofit2:converter-gson:2.3.0'

as per your given code still need GetScheduler Class code to understand which data your parsing. 按照您给定的代码,仍然需要GetScheduler类代码来了解您解析的数据。 but still if you get your Response then you can also parse that Data like 但是,即使您得到Response也可以像

List<GetScheduler> mclinet = null;
                    try {
                        Gson gson = new Gson();
                        Type type = new TypeToken<List<GetScheduler>>() {
                        }
                                .getType();
                        mclinet = gson.fromJson(YourresultString, type);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }

add in build.gradle 加入build.gradle

 compile 'com.google.code.gson:gson:2.6.2'

Your GetSheduler Class should be like with SerializedName with all required key, this just sample 您的GetSheduler类应该与带有所有必需键的SerializedName类似,这只是示例

public class GetSheduler{

    @SerializedName("light_id")
    @Expose
    private String light_id;
    @SerializedName("CunNm")
    @Expose
    private String cunNm;

    public String getCunID() {
        return light_id;
    }

    public void setCunID(String cunID) {
        this.light_id= cunID;
    }

    public String getCunNm() {
        return cunNm;
    }

    public void setCunNm(String cunNm) {
        this.cunNm = cunNm;
    }

}

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

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