【问题标题】:Not receiving json data with a REST call没有通过 REST 调用接收 json 数据
【发布时间】:2020-09-05 10:56:33
【问题描述】:

我正在尝试使用改造来使用我自己的 REST 调用。我关于它为什么不起作用的唯一线索是它说信息的类型是“text/html”,即使我确定它是 json。话虽如此,我还没有找到任何解决我问题的答案。

有效载荷:

  [
    {
        "user_id": "32",
        "username": "Marko",
        "last_activity": "2020-04-26 20:44:00",
        "user_image": "slika2"
    },
    {
        "user_id": "33",
        "username": "dejan",
        "last_activity": "2020-04-26 20:44:00",
        "user_image": "slika3"
    }
]

我的聊天课:

public class Chat {

    @SerializedName("user_id")
    private String userId;

    private String username;

    @SerializedName("last_activity")
    private String lastActivity;

    @SerializedName("user_image")
    private String userImage;\

    ...constructor/getters

   }

API接口:

@FormUrlEncoded
@POST("api_get_all_chats.php")
Call<List<Chat>> getChats(
        @Field("id") String id
);

API 客户端:

public static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        retrofit = new Retrofit.Builder()
                .baseUrl("http://MYIP/emob%20projekat/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        return retrofit;
    }

以及整个请求:

        apiInterface = ApiClient.getClient().create(userApi.class);

        Call<List<Chat>> call = apiInterface.getChats(u.getUserInfo().getUserId());

        call.enqueue(new Callback<List<Chat>>() {
            @Override
            public void onResponse(Call<List<Chat>> call, Response<List<Chat>> response) {
                chatList = new ArrayList<>(response.body());
                chatAdapter = new ChatAdapter(getApplication().getApplicationContext(), R.layout.user_view, chatList);
                listView.setAdapter(chatAdapter);
            }

            @Override
            public void onFailure(Call<List<Chat>> call, Throwable t) {
                Toast.makeText(getApplication().getApplicationContext(), "ne valja" , Toast.LENGTH_LONG).show();

            }
        });

任何我搞砸的线索都将不胜感激。

【问题讨论】:

  • 尝试在 POSTMAN 中运行相同的请求
  • 我做到了,而且效果很好。
  • 什么是ip?是本地主机还是普通 IP 或公共 IP?

标签: android json api rest retrofit


【解决方案1】:

当我解码时你的基本网址有空间我得到了

http://MYIP/emob projekat/api/

我建议您放置不编码的基本 URL

【讨论】:

  • 我会更新它,但它不会改变任何东西。我已经在使用同一文件夹中的一个 REST 调用,它可以正常工作。
【解决方案2】:

您的AndroidManifest 上的cleartextTrafficPermitted="true" 怎么样。我看到您的代码很好,我认为您的 URL 可能存在http 方案的问题。

【讨论】:

    【解决方案3】:

    您可以尝试在您的 API 接口中使用 Accept:application/json 指定您的客户期望的响应:

    @FormUrlEncoded
    @Headers({"Accept:application/json"})
    @POST("api_get_all_chats.php")
    Call<List<Chat>> getChats(
            @Field("id") String id
    );
    

    在您的 API 客户端中使用new Retrofit.Builder().addHeader("Accept","application/json");

    square/retrofit 中的 issue 与您的非常相似(只是相反,因为在问题中它是服务器不接受 text/html 而在您的情况下它似乎是您的客户端。也许,因为在这个问题中,您也使用了gson,它无法解析text/html 风味的响应)

    【讨论】:

      【解决方案4】:

      你需要这样的拦截器。

      public class CustomInterceptor extends Interceptor {
      
          @override 
          public Response intercept(Interceptor.Chain chain) {    
              return chain.proceed(chain.request().newBuilder().addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build())
          }
      }
      

      【讨论】:

        【解决方案5】:

        首先尝试记录请求,查看发送的确切内容,包括标头、url、正文和响应:https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor

        其次,你确定你必须发出一个 POST 请求,一个 GET 似乎更好吗?

        【讨论】:

          猜你喜欢
          • 2023-03-10
          • 2017-11-19
          • 2016-01-26
          • 2016-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多