Я пытаюсь поместить свои данные json в атрибут списка, но данные не отображаются в атрибуте.данные отображаются в окнах RUN.Как поместить данные в атрибуты?я использую фрагмент для отображения списка введите описание изображения здесь
Мой основной фрагмент:
api = retrofitClient.getClient("http://10.8.0.2:5000/").create(Api.class);
String encoding = Base64Encoder.encode(UtilsApi.username + ":" + UtilsApi.password);
Log.d(TAG, "listsensor: "+api.listSensor(UtilsApi.username,UtilsApi.password,"Basic "+encoding));
api.listSensor(UtilsApi.username, UtilsApi.password, "Basic " + encoding)
.enqueue(new Callback<List<Sensor>>() {
@Override
public void onResponse(Call<List<Sensor>> call, Response<List<Sensor>> response) {
if (response.isSuccessful()) {
try {
Toast.makeText(getActivity().getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
JSONObject jsonRESULTS = new JSONObject(response.body().toString());
sensors = (List<Sensor>) response.body();
listView.setAdapter(new SensorListAdapter(getActivity().getApplicationContext(), sensors));
}catch (JSONException e){
e.printStackTrace();
}
}else {
Toast.makeText(getActivity().getApplicationContext(), "Sensor Failed", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<List<Sensor>> call, Throwable t) {
Toast.makeText(getActivity().getApplicationContext(), "Sensor Failed", Toast.LENGTH_LONG).show();
Log.d(TAG, "INI: " + sensors);
}
});
}
SensorListAdapter:
private Context context;
private List<Sensor> sensors;
public SensorListAdapter(Context context, List<Sensor> sensors){
super(context, R.layout.list_sensor_item, sensors);
this.context = context;
this.sensors = sensors;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.list_sensor_item,parent,false);
Sensor sensor = sensors.get(position);
return convertView;
Интерфейс:
@FormUrlEncoded // List Sensor
@POST("/api/sensors/v1.0/listsensors")
Call<List<Sensor>> listSensor(
@Field("username") String username,
@Field("password") String password,
@Header("Authorization") String authtoken);
Retrofitclient
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
if (retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;