【问题标题】:response not coming from json using retrofit in android响应不是来自使用 android 中的改造的 json
【发布时间】:2019-05-09 01:44:36
【问题描述】:

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

但是我没有从数据中得到任何响应。使用该方法发送 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;
}

}

【问题讨论】:

  • 你在 postman 上测试过 api 吗?可能 light_id 为空,尝试记录或敬酒响应
  • 在调试模式控制下测试没有进入
  • 调用> call = service.getSchedulerData();
  • 添加 GsonConverterFactory 将 Json 数据转换为 Gson。
  • if (retrofit == null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); }

标签: android retrofit


【解决方案1】:

添加 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'

【讨论】:

  • 无法将builder中的addConverterFactory应用于(retrofit2.converter.gson.GsonConverterFactory)
  • 尝试使用相同版本的改造和 gson 转换器。编译 'com.squareup.retrofit2:retrofit:2.3.0' 编译 'com.squareup.retrofit2:converter-gson:2.3.0'
  • 你能分享你的应用依赖吗?
  • 你能看看我编辑的帖子吗。现在,我没有收到任何错误,但 Call> call = (Call>) service.getSchedulerData();在这条线控制将要异常块
【解决方案2】:

根据您给定的代码,仍然需要 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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 2019-03-14
    • 2019-02-26
    相关资源
    最近更新 更多