英文:
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("api/abcdefg")
Call<ResponseBody> 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("http://w.x.y.z:1111/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//ApiInterface jsonPlaceHolderApi = retrofit.create(ApiInterface.class);
ApiInterface api = retrofit.create(ApiInterface.class);
String json = "{\n" +
"\"nam\":\"Alex\",\n" +
"\"namk\":\"\"\n" +
"}";
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) {
}
});
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<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.d("RetrofitBruh", response.body().string());
textViewResult.setText(response.body().string()); //setting text here
} 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 = "{\n" +
"\"nam\":\"Alex\",\n" +
"\"namk\":\"\"\n" +
"}";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);
return requestBody;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论