Can anyone help me? I use Log.e("CHART_RESPONSE", "sus" );
to see where it executes, and it shows in OnFailure
. I’ve been trying to solve it for the past three days but it still couldn’t get it to work. Please help me.
This is API code:
public interface Api {
@FormUrlEncoded
@POST("register.php")
Call<RegisterResponse> register(
@Field("voltage") String voltage,
@Field("current") String current,
@Field("temp") String temp
);
@GET("fetch.php")
Call<ChartResponse> init();
}
RetrofitClient code:
package com.example.apiapp;
import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
private static OkHttpClient buildClient() {
return new OkHttpClient
.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
}
public static Retrofit getClient() {
if (retrofit == null) {
Gson gson=new Gson();
retrofit = new Retrofit.Builder()
.client(buildClient())
.addConverterFactory(GsonConverterFactory.create(gson))
/**
*
* base url here for api
*/
.baseUrl("http://192.168.1.103/PHP-API/")
.build();
}
return retrofit;
}
}
JSON code:
[
{
"voltage": [
"110.53",
"110.28",
"0.00",
"0.00",
"0.00"
]
},
{
"current": [
"10.79",
"4.93",
"100.80",
"0.00",
"0.00"
]
},
{
"temp": [
"28.54",
"34.00",
"35.77",
"0.00",
"0.00"
]
},
{
"hum": [
"31.00",
"90.13",
"3.80",
"0.00",
"0.00"
]
}
]
json pojo(I convert it from jsonschema2pojo):
public class ChartResponse{
@SerializedName("voltage")
@Expose
private static List<String> voltage;
@SerializedName("current")
@Expose
private static List<Float> current;
@SerializedName("temp")
@Expose
private static List<Float> temp;
@SerializedName("hum")
@Expose
private static List<Float> hum;
public static List<String> getVoltage() {
return voltage;
}
public void setVoltage(List<String> voltage) {
this.voltage = voltage;
}
public static List<Float> getCurrent() {
return current;
}
public void setCurrent(List<Float> current) {
this.current = current;
}
public static List<Float> getTemp() {
return temp;
}
public void setTemp(List<Float> temp) {
this.temp = temp;
}
public static List<Float> getHum() {
return hum;
}
public void setHum(List<Float> hum) {
this.hum = hum;
}
}
barchart code:
public class barchart extends AppCompatActivity {
private static Object data;
public Api api;
private Object chartResponse;
ArrayList<ChartResponse> chartList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barchart_main);
api = RetrofitClient.getClient().create(Api.class);
getData();
Button Hi =findViewById(R.id.button);
Hi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(barchart.this, MainActivity.class);
startActivity(intent);
}
});
}
/**
* api call to get some data from server
*/
private void getData() {
Call<ChartResponse> call = api.init();
call.enqueue(new Callback<ChartResponse>() {
@Override
public void onResponse(Call<ChartResponse> call, Response<ChartResponse> response) {
Log.e("CHART_RESPONSE", "sus" );
BarChart barchart = findViewById(R.id.fragment_verticalbarchart_chart);
Log.d("CHART_RESPONSE", "success" + response.body().getVoltage());
// ChartResponse chartresponse=response.body();
ArrayList<Float> dataSets = new ArrayList<Float>();
ArrayList<BarEntry> completed = new ArrayList<>();
/**
* Getting the value for Complete list
*/
for (int i = 0; i < 5; i++) {
BarEntry value = new BarEntry(ChartResponse.getHum().get(i), i);
completed.add(value);
}
for (int i = 0; i < dataSets.size(); i++) {
BarEntry barEntry = new BarEntry(i, dataSets.get(i));
completed.add(barEntry);
}
BarDataSet barDataSet = new BarDataSet(completed, "Completed Issue");
/**
* Add complete data into the bar chart
*/
barDataSet.setColor(Color.rgb(0, 155, 0));
BarData data = new BarData(barDataSet);
barchart.setData(data);
barchart.invalidate();
barchart.setData(data);
barchart.invalidate();
}
@Override
public void onFailure(Call<ChartResponse>call, Throwable t) {
Log.e("CHART_RESPONSE", "sfs" );
}
});
}
}
I’ve added:
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.squareup.okhttp3:okhttp:3.2.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.6.0'
and in manifest:
android:usesCleartextTraffic="true"
2
Answers
Rename Api interface to other name.
You may have imported another interface in your barchart Activity like (import com.google.android.gms.common.api.Api;
)
In
onFailure
, the second parameter is aThrowable
, which is the error that is thrown.Log that value, or set a debug breakpoint and check it out. It will tell you why it’s failing.
Always check any kind of Throwable/Exception.
Though i do see an error in your code.
All 4 arrays that are returned in your JSON response are List values, but you’ve set 3 of them as List values, that won’t work.
There still might be other errors though.