我面临一种奇怪的行为。我有一个应该返回json的查询。 这是我的界面:
public interface SubjectService {
@POST("/a/query/to/json?mobile=1")
Observable<SubjectRaw> getSubject(@Query("subjId") Integer subjId);
}
我称之为:
@Override
public Observable<SubjectRaw> loadData(Integer gcourse) {
return subjectService.getSubject(gcourse).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
这是我的ServiceGenerator类:
public class ServiceGenerator {
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass, String authString) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(chain -> {
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", " Basic " + authString).build();
return chain.proceed(request);
});
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
我也传递了一个基本授权字符串来进行改造。对于其他查询,它工作正常,但为此,我得到 null 。 邮差中的相同请求工作得很好: 我在这里缺少什么?
这是Logging-Interceptor的输出:
D/OkHttp: <-- 200 https://a/query/to/json?mobile=1&subjId=37622 (2025m
D/OkHttp: date: Fri, 31 Mar 2017 07:39:02 GMT
D/OkHttp: content-type: application/json
D/OkHttp: content-length: 4
D/OkHttp: set-cookie: __cfduid=da4b9463541e1542b6e983bc2b075652e1490945942; expires=Sat, 31-M
D/OkHttp: set-cookie: PHPSESSID=1a75rhfcvliejadv01rvmacpd0; path=/
D/OkHttp: expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: cache-control: no-store, no-cache, must-revalidate
D/OkHttp: pragma: no-cache
D/OkHttp: x-uversion: 2.1.2
D/OkHttp: server: cloudflare-nginx
D/OkHttp: cf-ray: 3481a70a9ea17600-ARN
D/OkHttp: null
D/OkHttp: <-- END HTTP (4-byte body)
答案 0 :(得分:0)
@Yordan提供了解决方案
尝试使用@FormUrlEncoded
装饰您的服务接口方法(getSubject)
由于他没有发表回答,我只是在这里张贴给任何可能遇到同样问题的人。 回答是@Yordan。