You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用@Path传入初始化整数时Retrofit返回null,直接传数值正常

Retrofit @Path传变量返回null,传固定值正常的排查方案

Hey there, I’ve run into a super similar issue with Retrofit and WordPress REST API before, so let’s break down what might be going on here and how to fix it.

1. 先确认变量number的实际值是否有效

First things first—you need to verify if the value returned by post.getId() is actually a valid WordPress media ID. Sometimes we assume we’re getting the right ID, but it could be a post ID (not media ID), a 0/negative number, or an ID for a media resource that’s been deleted.

Add a log line right before you call the API to check the value:

int number = post.getId();
Log.d(TAG, "Using media ID: " + number); // Compare this to the working fixed value 3658

If this number doesn’t match a valid media ID, WordPress API will return a 404 response, and response.body() will naturally be null.

2. Print the full request URL to spot differences

Retrofit can sometimes have subtle path concatenation issues, especially if your BASE_URL isn’t formatted correctly. Add a logging interceptor to see the exact URL generated for both calls.

First, add the OkHttp logging dependency (if you haven’t already), then update your ImageFactory:

// Create a logging interceptor
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY); // Log full request/response details

OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(logging)
    .build();

// Update the Retrofit builder to use this client
Retrofit retrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(BASE_URL)
    .client(client) // Add this line to enable logging
    .build();

Run your app and check the logs—compare the full URL when using the variable vs. the fixed 3658. If the URL has an incorrect ID or malformed path, that’s your issue.

3. Fix your BASE_URL format

Your current BASE_URL has multiple consecutive slashes //:

String BASE_URL = "https://dwarkawala.com/index.php/wp-json//wp//v2//";

While Retrofit tries to handle extra slashes automatically, it can still cause unexpected path issues. Switch to a clean, standard format with single slashes between path segments (keep a trailing slash):

String BASE_URL = "https://dwarkawala.com/index.php/wp-json/wp/v2/";

4. Check for integer overflow issues

If post.getId() returns a long type, converting it to int could cause overflow (if the ID exceeds the int limit of 2147483647). This would result in an invalid, possibly negative number being passed to the API.

Update your interface to accept a long parameter instead:

@GET("media/{Id}")
Call<Thumbnail> getImageData(
    @Path("Id") long postId // Change int to long
);

Then call it directly with post.getId() (no need to cast to int if it’s already a long).

5. Inspect the response status code and error details

Don’t just check response.body()—log the status code and error body to get more context:

@Override
public void onResponse(Call<Thumbnail> call, retrofit2.Response<Thumbnail> response) {
    Log.d(TAG, "Response code: " + response.code());
    Log.d(TAG, "Response message: " + response.message());
    
    if (!response.isSuccessful()) {
        // Parse the error body to see what WordPress is telling you
        try {
            String errorDetails = response.errorBody().string();
            Log.d(TAG, "Error details: " + errorDetails);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    Log.d(TAG, String.valueOf(response.body()));
}

If you see a 404, the ID is invalid; a 400 means there’s a parameter format issue. This will help you pinpoint the exact problem.


内容的提问来源于stack exchange,提问作者Rahul Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:15:23