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

时间:2018-12-07 09:50:59

标签: android retrofit

您好,下面的代码将发送方法作为get并从中获取响应,并希望显示要设置为变量的响应。

但是没有从data.am获得任何响应。使用该方法发送get方法正在获取响应。响应成功后,然后要将文本设置为textviews

API.java:

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

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

}

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:

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

}

2 个答案:

答案 0 :(得分:0)

添加GsonConverter将json数据转换为Pojo类。

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

并在build.gradle文件中添加依赖项

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

答案 1 :(得分:0)

根据您给定的代码,仍然需要GetScheduler类代码来了解要解析的数据。但即使您得到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();
                    }

添加build.gradle

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

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

}