无法在 android 中使用 retrofit2 发出 @Post 请求
Not able to make @Post request using retrofit2 in android
我正在学习如何在 android 中使用改造,但每当我尝试从互联网检索数据时,我的应用程序都没有 return 任何东西 我的回复没有成功,我也没有目前我不知道如何修复错误 我正在尝试 post 并使用 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);
}
由于未知原因 https://jsonplaceholder.typicode.com
抛出套接字超时异常,同时您可以检查另一个具有类似功能的网站 https://reqres.in/
这里是 post [=] 附带的示例代码15=]
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 + '\'' +
'}';
}
}
我正在学习如何在 android 中使用改造,但每当我尝试从互联网检索数据时,我的应用程序都没有 return 任何东西 我的回复没有成功,我也没有目前我不知道如何修复错误 我正在尝试 post 并使用 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);
}
由于未知原因 https://jsonplaceholder.typicode.com
抛出套接字超时异常,同时您可以检查另一个具有类似功能的网站 https://reqres.in/
这里是 post [=] 附带的示例代码15=]
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 + '\'' +
'}';
}
}