gpt4 book ai didi

java - 如何在属性中显示json数据

转载 作者:行者123 更新时间:2023-12-02 03:39:55 28 4
gpt4 key购买 nike

我尝试将 json 数据放入 listview 属性中,但数据未显示在属性中。数据显示在RUN窗口中。如何将数据放入属性中?我使用 fragment 来显示 ListView enter image description here

我的主要 fragment :

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

传感器列表适配器:

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

改造客户端

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;

最佳答案

根据 Json 结构,List 本身是外部 Json 对象的一部分。假设 SensorsObject,因此使用 SensorsObject 作为响应,List 将是此类的属性。

关于java - 如何在属性中显示json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841944/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com