android - 改造后请求结构

标签 android json url post retrofit2

我需要在 android 中发出 POST 请求。在我在 Postman 中尝试之前,它工作正常。

enter image description here

但在 Android 中(我使用的是 Retrofit2)它不想连接服务器。

我的 API 服务:

@POST("home/info/")
Call<ResponseData> getJson(@Body Post post);

我的 RetrofitClient:

Retrofit.Builder()
            .baseUrl(http://api.beauty.dikidi.ru/)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

我的贴体类

private String city_id;

public String getCity_id() {
    return city_id;
}

public void setCity_id(String city_id) {
    this.city_id = city_id;
}

我尝试了不同的解决方案:@Query、@Field。我尝试使用类似 here 的 URL .未达到我在 OnResponse 中的断点。请帮我建立连接!

来自拦截器的日志

D/OkHttp: <-- 200 OK http://api.beauty.dikidi.ru/home/info/ (474ms)
Server: nginx
Date: Wed, 14 Mar 2018 09:56:27 GMT
Content-Type: application/json; charset="utf-8"
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Set-Cookie: lang=208f1b939dfd656bcfad0eac6c66f41806155878%7Eru; path=/;       domain=.dikidi.ru; HttpOnly
03-14 09:56:25.913 5997-6029/example.TestProject D/OkHttp: 
Expires: Mon, 26 Jul 1990 05:00:00 GMT
Last-Modified: Wed, 14 Mar 2018 09:56:27 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: 
cookie_name=3b9f5f43b88487ff1e44e0f6da790f25a8913101%7E5aa8f1cb5b7c31-
92789521; expires=Thu, 15-Mar-2018 09:56:27 GMT; Max-Age=86400; path=/; 
domain=.dikidi.ru; HttpOnly
03-14 09:56:25.914 5997-6029/maxzonov.modersoft_testproject D/OkHttp: {"error":{"code":1,"message":"\u041e\u0448\u0438\u0431\u043a\u0430. city_id - \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440!"},"data":[]}

显示连接正常,但参数传递错误。

The problem with passing parametres was resolved. Now code inside in Retrofit is not called.

调用 fragment :

    ApiService apiService = RetrofitClient.getApiService();
    Call<ResponseData> call = apiService.getJson(CITY_ID);
    call.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
            int status = response.code();
            String count = response.body().getData().getBlock().getShare().getCount();
            Log.d("myLog", count);
            getViewState().showShare(count);
        }

        @Override
        public void onFailure(Call<ResponseData> call, Throwable t) {

        }
    });

最佳答案

试试这个

@POST("home/info/{city_id}")
Call<ResponseData> getData(@Path("city_id") int cityId);

关于android - 改造后请求结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49273760/

相关文章:

android - Google Play 应用搜索过滤器

选择输入框上的 jQuery UI 自动完成

python - 根据 for 循环中的迭代次数向列表添加不同的值

ios - 我的 url 不返回 nil,而是快速返回空字符串

java - 在哪里以及如何解码@PathVariable

android - 合并两个位图并保存为一个图像

安卓蓝牙 LE SCAN_FAILED_OUT_OF_HARDWARE_RESOURCES

javascript - 使用 JSON.stringify() 和 JSON.parse() 时的 Date() 问题

php - 在 Slim Framework v3 中访问多个 GET 参数

java - 如何在 Android Studio 中的 datasnapshot/recyclerview 内创建条件?