【问题标题】:Retrofit does not send header改造不发送标头
【发布时间】:2016-10-06 09:48:34
【问题描述】:


我目前正在开发一个 Android 应用程序,但我遇到了一个问题,我正在尝试在我的请求中发送一个带有改造的标头,但是当我使用 PHP 检查我的服务器时,它看起来标头甚至不存在。
这是我的代码:
Android

@Headers("SECRET_KEY: QWERTZUIOP")
@GET("{TableName}/")
Call<List<Data>> cl_getAllFromTable(@Path("TableName") String TableName);


PHP 服务器

$secret_key = $_SERVER['HTTP_SECRET_KEY'];


如果有人可以提供帮助,我会很高兴。提前致谢。
逗弄

【问题讨论】:

标签: php android header retrofit


【解决方案1】:
// Define the interceptor, add authentication headers
Interceptor interceptor = new Interceptor() {
       @Override
       public okhttp3.Response intercept(Chain chain) throws IOException {
          Request newRequest = chain.request().newBuilder().addHeader("User-Agent", "Retrofit-Sample-App").build();
          return chain.proceed(newRequest);
       }
};

// Add the interceptor to OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
OkHttpClient client = builder.build();

// Set the custom client when building adapter
Retrofit retrofit = new Retrofit.Builder()
       .baseUrl("https://api.github.com")
       .addConverterFactory(GsonConverterFactory.create())
       .client(client)
       .build();

参考:https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

【讨论】:

  • 是真的,改造不不显示headers就很糟糕了
猜你喜欢
  • 2020-04-26
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多