当 是Javabean 时就能得到回调.
之前提问匆忙没有贴代码,好多人要是没遇上还真不知道我在说啥.
代码片段如下:
public void getDemoData(Subscriber<String> subscriber){
HttpService.getInstance()
.create(DemoService.class)
.getDemoPicture("params1",params2,params3,"params4")
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
private void requestData() {
HttpMethods.getInstance().getDemoData(new Subscriber<String>() {
@Override
public void onCompleted() {
Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
showToast(getResources().getString(R.string.error_data));
}
@Override
public void onNext(String str) {
//....
}
}
});
}
private HttpService() {
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClientBuilder.addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BingUrl.BING)
.build();
}
HttpLoggingInterceptor 能打印出请求成功状态和返回的数据,只是回调都没走
I have also encountered this problem, that is, when an error occurs, the object is not returned, but a string is returned, and then it cannot be parsed. Currently, it only prompts a hard-coded error message, or directly uses string parsing. I don’t know if there is any way to solve it
You can definitely get it by returning String. Post the source code. How did you write it
If something goes wrong, handle it in onError
The parameter passed in to getDemoData in the first method should be written incorrectly. It should be Subscriber<
DemoService
> subscriber,而不是Subscriber<String
> subscriber