【发布时间】:2020-05-28 21:15:40
【问题描述】:
我正在学习如何在 android 中使用改造,但每当我尝试从 Internet 检索数据时,我的应用程序不会返回任何内容我的响应没有成功,我不知道如何修复当前的错误尝试使用 https://jsonplaceholder.typicode.com 从该 URL 发布和检索数据。我无法弄清楚是什么原因造成的。
主活动
private void createpost() {
Post post2=new Post(1,"This is Title","10");
Call<Post> postCall= mWebService.createPost(post2);
postCall.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
if (response.isSuccessful())
{
Log.d(TAG,"response.isSuccessful()");
mLog.setText(String.valueOf(response.code()));
showPost(response.body());
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
}
});
}
界面
public interface MyWebService {
String BASE_URL="https://jsonplaceholder.typicode.com/";
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
@POST("posts")
Call<Post> createPost(@Body Post post);
}
【问题讨论】: