英文:
Not able to make @Post request using retrofit2 in android
问题
我正在学习如何在 Android 中使用 Retrofit,但每当我尝试从互联网检索数据时,我的应用程序没有返回任何内容。我的响应没有成功返回,我不知道如何修复这个错误,目前我正在尝试使用 https://jsonplaceholder.typicode.com 这个 URL 发布和检索数据。我无法弄清楚是什么原因引起了这个问题。
> MainActivity
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) {
}
});
}
> Interface
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);
}
英文:
I am learning how to use retrofit in android but whenever I try to retrieve data from the internet my app doesn't return anything My response doesn't come successful and I don't know how to fix the error currently I am trying to post and retrieve data from this URL using https://jsonplaceholder.typicode.com. I am unable to figure out what is causing this.
> MainActivity
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) {
}
});
}
> Interface
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);
}
答案1
得分: 3
以下是翻译好的部分:
出现未知原因的情况下,`https://jsonplaceholder.typicode.com` 抛出了套接字超时异常,与此同时,您可以检查另一个具有类似功能的网站 `https://reqres.in/`,以下是附在帖子中的示例代码:
```java
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build();
final String host = "https://reqres.in/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(HttpUrl.parse(host))
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
ReqResApi api = retrofit.create(ReqResApi.class);
api.createUser(new User("silentsudo", "dev"))
.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
System.out.println("响应成功: " + response.body().toString());
}
@Override
public void onFailure(Call<User> call, Throwable throwable) {
System.err.println("调用 API 时出错");
throwable.printStackTrace();
}
});
API
interface ReqResApi {
@POST("api/user")
Call<User> createUser(@Body User post);
}
POJO
class User {
private String name;
private String job;
public User(String name, String job) {
this.name = name;
this.job = job;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
'}';
}
}
<details>
<summary>英文:</summary>
For unknown reason `https://jsonplaceholder.typicode.com` is throwing socket timeout exception, in the mean time you can check another website with similar functionality `https://reqres.in/` here is the sample code attached to post
```java
final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build();
final String host = "https://reqres.in/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(HttpUrl.parse(host))
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
ReqResApi api = retrofit.create(ReqResApi.class);
api.createUser(new User("silentsudo", "dev"))
.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
System.out.println("Resp Success full: " + response.body().toString());
}
@Override
public void onFailure(Call<User> call, Throwable throwable) {
System.err.println("Error calling api");
throwable.printStackTrace();
}
});
Api
interface ReqResApi {
@POST("api/user")
Call<User> createUser(@Body User post);
}
POJO
class User {
private String name;
private String job;
public User(String name, String job) {
this.name = name;
this.job = job;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
'}';
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论