0

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.

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

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 Answers2

0

Add GsonConverter to convert json data to Pojo classes.

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

and add dependency in build.gradle file

compile 'com.squareup.retrofit2:converter-gson:2.3.0'
Arvind
  • 434
  • 4
  • 14
  • addConverterFactory in builder cannot be applied to (retrofit2.converter.gson.GsonConverterFactory) – jyothi chandra Dec 07 '18 at 10:25
  • try to use same version of retrofit and gson converter. compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0' – Arvind Dec 07 '18 at 10:29
  • Check this for more details: https://square.github.io/retrofit/ and https://medium.com/@prakash_pun/retrofit-a-simple-android-tutorial-48437e4e5a23 – Arvind Dec 07 '18 at 10:47
  • can you share your app dependencies? – Arvind Dec 07 '18 at 11:53
  • can you please see my edited post.Now, am not getting any error but Call> call = (Call>) service.getSchedulerData(); After this line control was going to exception block – jyothi chandra Dec 07 '18 at 11:59
  • What exception are you getting? Print exception value. – Arvind Dec 07 '18 at 12:03
  • java.lang.IllegalArgumentException: Unable to create call adapter for retrofit.Call> for method API.getSchedulerData – jyothi chandra Dec 07 '18 at 12:13
  • Check this answer: https://stackoverflow.com/questions/32269064/unable-to-create-call-adapter-for-class-example-simple – Arvind Dec 07 '18 at 12:20
0

as per your given code still need GetScheduler Class code to understand which data your parsing. but still if you get your Response then you can also parse that Data like

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

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

Your GetSheduler Class should be like with SerializedName with all required key, this just sample

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

}
Ankitkumar Makwana
  • 3,724
  • 3
  • 18
  • 45