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

Retrofit GET Query将=转为=致API响应错误,请求协助解决

Troubleshooting Retrofit Query Mismatch: Same URL, Different API Response

Hey there! Let’s figure out why your Retrofit call is returning a different result even though the URL looks identical to the one you tested directly. This kind of issue usually boils down to subtle differences between the manual request and what Retrofit is actually sending—let’s break down the steps to fix it:

First: Verify the Exact Request Retrofit is Sending

The first rule of debugging API discrepancies is to see the full request details. Add OkHttp’s logging interceptor to your Retrofit setup to print every part of the request (URL, headers, parameters):

// Add the logging interceptor
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

// Build your OkHttpClient with the interceptor
OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .build();

// Attach to Retrofit
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://xxxxx/")
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

Run your app again and check the logs. Compare every detail (headers, parameter encoding, even the order of parameters) to the request you tested manually (e.g., in Postman or a browser). You’ll likely spot the difference here.

Common Fixes Based on Likely Issues

1. Parameter Encoding or Type Mismatch

Even if the URL looks the same, Retrofit might be encoding parameters in a way the server doesn’t expect, or you might be passing the wrong data type:

  • If taxYear should be an integer, make sure your Retrofit interface defines it as an Integer instead of a String:
    @GET("Status")
    Call<YourResponseModel> checkStatus(@Query("taxYear") Integer taxYear);
    
  • Try disabling parameter encoding explicitly (useful if the server expects raw, unencoded values):
    @GET("Status")
    Call<YourResponseModel> checkStatus(@Query(value = "taxYear", encoded = true) String taxYear);
    

2. Missing or Mismatched Request Headers

Browsers and tools like Postman automatically add headers (e.g., User-Agent, Accept) that Retrofit doesn’t include by default. Servers often use these headers to route requests or apply different logic:

  • Replicate the headers from your working manual request in Retrofit. You can add them directly to the interface:
    @GET("Status")
    Call<YourResponseModel> checkStatus(
        @Query("taxYear") String taxYear,
        @Header("User-Agent") String userAgent,
        @Header("Accept") String acceptHeader
    );
    
  • Or use an interceptor to apply headers to all requests:
    Interceptor headerInterceptor = chain -> {
        Request modifiedRequest = chain.request().newBuilder()
            .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
            .header("Accept", "*/*")
            .build();
        return chain.proceed(modifiedRequest);
    };
    
    // Add this interceptor to your OkHttpClient
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addInterceptor(loggingInterceptor)
        .addInterceptor(headerInterceptor)
        .build();
    

3. Cache Interference

Your manual request might be hitting a cached response, while Retrofit is sending a fresh request (or vice versa). Disable caching temporarily to rule this out:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .cache(null) // Disable cache entirely
    .build();

Alternatively, add a Cache-Control: no-cache header to force the server to return fresh data.

4. Double-Check QueryMap Usage

If you tried QueryMap, ensure your map has the exact key-value pair without typos (case sensitivity matters!):

@GET("Status")
Call<YourResponseModel> checkStatus(@QueryMap Map<String, Object> params);

// When calling the method:
Map<String, Object> params = new HashMap<>();
params.put("taxYear", 2017); // Use Integer instead of String if needed
Call<YourResponseModel> call = yourApiService.checkStatus(params);

Final Tip

Once you use the logging interceptor to spot the exact difference between the working and failing requests, fixing it will be straightforward. The most common culprits are missing headers or unexpected parameter encoding.

内容的提问来源于stack exchange,提问作者Suseendran Kandasamy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:00:56