无法在安卓中使用Retrofit2进行@Post请求

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

Not able to make @Post request using retrofit2 in android

问题

  1. 我正在学习如何在 Android 中使用 Retrofit但每当我尝试从互联网检索数据时我的应用程序没有返回任何内容我的响应没有成功返回我不知道如何修复这个错误目前我正在尝试使用 https://jsonplaceholder.typicode.com 这个 URL 发布和检索数据。我无法弄清楚是什么原因引起了这个问题。
  2. > MainActivity
  3. private void createpost() {
  4. Post post2 = new Post(1, "This is Title", "10");
  5. Call<Post> postCall = mWebService.createPost(post2);
  6. postCall.enqueue(new Callback<Post>() {
  7. @Override
  8. public void onResponse(Call<Post> call, Response<Post> response) {
  9. if (response.isSuccessful()) {
  10. Log.d(TAG, "response.isSuccessful()");
  11. mLog.setText(String.valueOf(response.code()));
  12. showPost(response.body());
  13. }
  14. }
  15. @Override
  16. public void onFailure(Call<Post> call, Throwable t) {
  17. }
  18. });
  19. }
  20. > Interface
  21. public interface MyWebService {
  22. String BASE_URL = "https://jsonplaceholder.typicode.com/";
  23. Retrofit retrofit = new Retrofit.Builder()
  24. .baseUrl(BASE_URL)
  25. .addConverterFactory(GsonConverterFactory.create())
  26. .build();
  27. @POST("posts")
  28. Call<Post> createPost(@Body Post post);
  29. }
英文:

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

  1. private void createpost() {
  2. Post post2=new Post(1,&quot;This is Title&quot;,&quot;10&quot;);
  3. Call&lt;Post&gt; postCall= mWebService.createPost(post2);
  4. postCall.enqueue(new Callback&lt;Post&gt;() {
  5. @Override
  6. public void onResponse(Call&lt;Post&gt; call, Response&lt;Post&gt; response) {
  7. if (response.isSuccessful())
  8. {
  9. Log.d(TAG,&quot;response.isSuccessful()&quot;);
  10. mLog.setText(String.valueOf(response.code()));
  11. showPost(response.body());
  12. }
  13. }
  14. @Override
  15. public void onFailure(Call&lt;Post&gt; call, Throwable t) {
  16. }
  17. });
  18. }

> Interface

  1. public interface MyWebService {
  2. String BASE_URL=&quot;https://jsonplaceholder.typicode.com/&quot;;
  3. Retrofit retrofit=new Retrofit.Builder()
  4. .baseUrl(BASE_URL)
  5. .addConverterFactory(GsonConverterFactory.create())
  6. .build();
  7. @POST(&quot;posts&quot;)
  8. Call&lt;Post&gt; createPost(@Body Post post);
  9. }

答案1

得分: 3

以下是翻译好的部分:

  1. 出现未知原因的情况下,`https://jsonplaceholder.typicode.com` 抛出了套接字超时异常,与此同时,您可以检查另一个具有类似功能的网站 `https://reqres.in/`,以下是附在帖子中的示例代码:
  2. ```java
  3. final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
  4. httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  5. final OkHttpClient okHttpClient = new OkHttpClient.Builder()
  6. .addInterceptor(httpLoggingInterceptor)
  7. .build();
  8. final String host = "https://reqres.in/";
  9. Retrofit retrofit = new Retrofit.Builder()
  10. .baseUrl(HttpUrl.parse(host))
  11. .client(okHttpClient)
  12. .addConverterFactory(GsonConverterFactory.create())
  13. .build();
  14. ReqResApi api = retrofit.create(ReqResApi.class);
  15. api.createUser(new User("silentsudo", "dev"))
  16. .enqueue(new Callback<User>() {
  17. @Override
  18. public void onResponse(Call<User> call, Response<User> response) {
  19. System.out.println("响应成功: " + response.body().toString());
  20. }
  21. @Override
  22. public void onFailure(Call<User> call, Throwable throwable) {
  23. System.err.println("调用 API 时出错");
  24. throwable.printStackTrace();
  25. }
  26. });

API

  1. interface ReqResApi {
  2. @POST("api/user")
  3. Call<User> createUser(@Body User post);
  4. }

POJO

  1. class User {
  2. private String name;
  3. private String job;
  4. public User(String name, String job) {
  5. this.name = name;
  6. this.job = job;
  7. }
  8. @Override
  9. public String toString() {
  10. return "User{" +
  11. "name='" + name + '\'' +
  12. ", job='" + job + '\'' +
  13. '}';
  14. }
  15. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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
  4. ```java
  5. final HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
  6. httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  7. final OkHttpClient okHttpClient = new OkHttpClient.Builder()
  8. .addInterceptor(httpLoggingInterceptor)
  9. .build();
  10. final String host = &quot;https://reqres.in/&quot;;
  11. Retrofit retrofit = new Retrofit.Builder()
  12. .baseUrl(HttpUrl.parse(host))
  13. .client(okHttpClient)
  14. .addConverterFactory(GsonConverterFactory.create())
  15. .build();
  16. ReqResApi api = retrofit.create(ReqResApi.class);
  17. api.createUser(new User(&quot;silentsudo&quot;, &quot;dev&quot;))
  18. .enqueue(new Callback&lt;User&gt;() {
  19. @Override
  20. public void onResponse(Call&lt;User&gt; call, Response&lt;User&gt; response) {
  21. System.out.println(&quot;Resp Success full: &quot; + response.body().toString());
  22. }
  23. @Override
  24. public void onFailure(Call&lt;User&gt; call, Throwable throwable) {
  25. System.err.println(&quot;Error calling api&quot;);
  26. throwable.printStackTrace();
  27. }
  28. });

Api

  1. interface ReqResApi {
  2. @POST(&quot;api/user&quot;)
  3. Call&lt;User&gt; createUser(@Body User post);
  4. }

POJO

  1. class User {
  2. private String name;
  3. private String job;
  4. public User(String name, String job) {
  5. this.name = name;
  6. this.job = job;
  7. }
  8. @Override
  9. public String toString() {
  10. return &quot;User{&quot; +
  11. &quot;name=&#39;&quot; + name + &#39;\&#39;&#39; +
  12. &quot;, job=&#39;&quot; + job + &#39;\&#39;&#39; +
  13. &#39;}&#39;;
  14. }
  15. }

huangapple
  • 本文由 发表于 2020年5月29日 05:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074649.html
匿名

发表评论

匿名网友

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

确定