如何在属性中显示JSON数据

时间:2019-07-01 20:19:33

标签: java android listview retrofit2

我正在尝试将json数据放到listview属性中,但数据不会显示在属性中。数据显示在RUN窗口中。 如何将数据放入属性?我用片段来显示列表视图 enter image description here

我的主要片段:

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;

1 个答案:

答案 0 :(得分:0)

按照Json结构,List本身是外部Json Object的一部分。假设说SensorsObject,所以使用SensorsObject作为响应,并且List将成为此类的属性。