简体   繁体   English

如何在 android 中使用 GET 方法在多个文本视图中显示值?

[英]How to display value in multiple textviews using GET method in android?

What I am trying to do is, fetch the datas from server using GET method and display them in multiple textfields.我想要做的是,使用 GET 方法从服务器获取数据并将它们显示在多个文本字段中。 I have already made model class, interface and created constructor, but still the app throws onFailure message.我已经制作了模型类、接口并创建了构造函数,但应用程序仍然会抛出 onFailure 消息。

TextView name;

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

String url="https://jsonplaceholder.typicode.com/";

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    name = view.findViewById(R.id.proName);
    name.setText("");


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    PlaceHolderApi placeHolderApi = retrofit.create(PlaceHolderApi.class);

    Call<List<profileDetails>> call = placeHolderApi.getDetails();
    call.enqueue(new Callback<List<profileDetails>>() {
        @Override
        public void onResponse(Call<List<profileDetails>> call, Response<List<profileDetails>> response) {
            List<profileDetails> data=response.body();

            **for (int i=0; i<data.size();i++)
                name.append("Aa"+ data.get(i).getTitle());**

        }

        @Override
        public void onFailure(Call<List<profileDetails>> call, Throwable t) {

            Toast.makeText(getContext(),"nah it",Toast.LENGTH_LONG).show();
        }
    });
}

} }

The result you are getting is a json object so try to create a model class having a data member of type List and making a call of type List make a call of that model type.您得到的结果是一个 json 对象,因此请尝试创建一个具有 List 类型数据成员的模型类,并调用 List 类型来调用该模型类型。 This may resolve your issue.这可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM