在TextView中显示Android中的数据

huangapple go评论73阅读模式
英文:

retrofit android display data in textview

问题

Sure, here is the translated code portion:

我的问题可能看起来很基础
我有这个Post请求
我在Retrofit2/Java Android中有点新

@POST("api/abcdefg")
Call<ResponseBody> postResponse(@Body RequestBody requestBody);

以及这个数据模型

public class Post {
    private int id;
    private String nam;
    private String namk;
    private String namp;

    public int getId() {
        return id;
    }

    public String getNam() {
        return nam;
    }

    public String getNamk() {
        return namk;
    }

    public String getNamp() {
        return namp;
    }
}

我想在TextView中显示它们但目前只能在Log.d中显示它们
这是我的主要代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewResult = findViewById(R.id.text_view_result);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://w.x.y.z:1111/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    //ApiInterface jsonPlaceHolderApi = retrofit.create(ApiInterface.class);

    ApiInterface api = retrofit.create(ApiInterface.class);

    String json = "{" +
            "\"nam\":\"Alex\"," +
            "\"namk\":\"\"" +
            "}";

    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);

    api.postResponse(requestBody).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.d("RetrofitBruh", response.body().string());
            } catch (IOException e){
                e.printStackTrace();
            }
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
        }
    });
}

我知道这是一个简单的问题
谢谢你的回复 
抱歉我占用了你的时间

Please note that the code provided is a translation of the original code you posted. If you have any specific questions or need further assistance with this code, please let me know.

英文:

My questions may seem rudimentary
i have this Post Req
I'm kinda new in Retrofit2/javaAndroid

@POST(&quot;api/abcdefg&quot;)
Call&lt;ResponseBody&gt; postResponse(@Body RequestBody requestBody);

and this dataModel

public class Post {
private int id;
private String nam;
private String namk;
private String namp;
public int getId() {
return id;
}
public String getText() {
return text;
}
public String getNam() {
return nam;
}
public String getNamk() {
return namk;
}
public String getNamp() {
return namp;
}
}

I want to display them in a textview, but for now I can only display them in the Log.d
and here is my main

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult = findViewById(R.id.text_view_result);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(&quot;http://w.x.y.z:1111/&quot;)
.addConverterFactory(GsonConverterFactory.create())
.build();
//ApiInterface jsonPlaceHolderApi = retrofit.create(ApiInterface.class);
ApiInterface api = retrofit.create(ApiInterface.class);
String json = &quot;{\n&quot; +
&quot;\&quot;nam\&quot;:\&quot;Alex\&quot;,\n&quot; +
&quot;\&quot;namk\&quot;:\&quot;\&quot;\n&quot; +
&quot;}&quot;;
RequestBody requestBody = RequestBody.create(MediaType.parse(&quot;application/json&quot;),json);
api.postResponse(requestBody).enqueue(new Callback&lt;ResponseBody&gt;() {
@Override
public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
try {
Log.d(&quot;RetrofitBruh&quot;, response.body().string());
} catch (IOException e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
}
});

I know it's a simple question
Thanks for replying ♥
Sorry , i took your time

答案1

得分: 1

考虑到您在布局中有一个 TextView 用于显示,然后您可以执行以下操作。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 用于显示来自 API 的结果的 TextView
    textViewResult = findViewById(R.id.text_view_result);
    // 在这里调用 API
    getApiResults();
}

private void getResults() {
    getApiService().postResponse(getRequestModel()).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.d("RetrofitBruh", response.body().string());
                // 在此设置文本
                textViewResult.setText(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

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

        }
    });
}

private ApiInterface getApiService() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://w.x.y.z:1111/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ApiInterface api = retrofit.create(ApiInterface.class);
    return api;
}

private RequestBody getRequestModel() {
    String json = "{" +
            "\"name\":\"Alex\"," +
            "\"namk\":\"\"" +
            "}";
    
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
    return requestBody;
}

注意:此处返回的是代码的翻译。

英文:

considering you have a TextView in the layout for showing, then you can do something.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//text view for showing results from api
textViewResult = findViewById(R.id.text_view_result);
//here call the api
getApiResults();
}
private void getResults(){
getApiService().postResponse(getRequestModel()).enqueue(new Callback&lt;ResponseBody&gt;() {
@Override
public void onResponse(Call&lt;ResponseBody&gt; call, Response&lt;ResponseBody&gt; response) {
try {
Log.d(&quot;RetrofitBruh&quot;, response.body().string());
textViewResult.setText(response.body().string()); //setting text here
} catch (IOException e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call&lt;ResponseBody&gt; call, Throwable t) {
}
});
}
private ApiInterface getApiService(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(&quot;http://w.x.y.z:1111/&quot;)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface api = retrofit.create(ApiInterface.class);
return api;
}
private RequestBody getRequestModel(){
String json = &quot;{\n&quot; +
&quot;\&quot;nam\&quot;:\&quot;Alex\&quot;,\n&quot; +
&quot;\&quot;namk\&quot;:\&quot;\&quot;\n&quot; +
&quot;}&quot;;
RequestBody requestBody = RequestBody.create(MediaType.parse(&quot;application/json&quot;),json);
return requestBody;
}

huangapple
  • 本文由 发表于 2020年8月8日 21:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定