我在使用rx android和改型时遇到网络线程异常。这是我的代码:
public Observable<PostVariablesModel> requestApiItem() {
return getauthenticationtoken.getToken().flatMap(new Func1<String, Observable<PostVariablesModel>>() {
public Observable<PostVariablesModel> call(String token) {
return service.postdata(value1, value2, value3, token);
}
}).subscribeOn(Schedulers.io());
}
public void postdata() {
requestApiItem() //I think problem is here any idea?
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<PostVariablesModel>() {
@Override
public void onCompleted() {
System.out.print("Complete");
}
@Override
public void onError(Throwable e) {
System.out.print("Fail");
}
@Override
public void onNext(PostVariablesModel apiResult) {
System.out.print(apiResult.toString());
}
});
}
我们必须在完成http调用后取消订阅吗。知道吗?
异常如下所示。我在代码中的postdata方法的onError上得到这个异常:
android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at okhttp3.Dns$1.lookup(Dns.java:39)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:170)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:136)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:81)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
at okhttp3.RealCall.execute(RealCall.java:63)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.Subscriber.setProducer(Subscriber.java:205)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:8666)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:250)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:147)
at rx.internal.operators.OperatorMap$MapSubscriber.onNext(OperatorMap.java:74)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:227)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
试试这个
public Observable<PostVariablesModel> requestApiItem() {
return getauthenticationtoken.getToken().flatMap(new Func1<String, Observable<PostVariablesModel>>() {
public Observable<PostVariablesModel> call(String token) {
return service.postdata(value1, value2, value3, token);
}
});
}
public void postdata() {
requestApiItem() //I think problem is here any idea?
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<PostVariablesModel>() {
@Override
public void onCompleted() {
System.out.print("Complete");
}
@Override
public void onError(Throwable e) {
System.out.print("Fail");
}
@Override
public void onNext(PostVariablesModel apiResult) {
System.out.print(apiResult.toString());
}
});
}
编辑您可以像这样尝试函数Request estApiItem()
:
public Observable<PostVariablesModel> requestApiItem() {
return service.postdata(value1, value2, value3, token)
.flatMap(new Func1<PostVariablesModel>() {
@Override
public Observable<? extends PostVariablesModel> call(PostVariablesModel yourModel) {
return Observable.just(yourModel);
}
});
}
注意:您可以尝试上面的代码(将返回的数据更改为您的模型)。
return getauthenticationtoken.getToken().subscribeOn(Schedulers.io());
但是您也可以使用默认调度程序创建调用适配器,它可以是Schedulers.io()。
return new Retrofit.Builder()
.addConverterFactory(SimpleXmlConverterFactory.create())
.baseUrl("http://thecatapi.com/")
.addCallAdapterFactory(
RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())) // <---- !!!
.build()
我需要做HTTP删除请求到我的服务器与身体提供。 我以以下方式构建改型对象:
RxAndroid 是 RxJava 的 Android 绑定。
为什么我们需要将RxAndroid与RxJava一起使用?它们与实际使用RxAndroid和RxJava之间的功能差异是什么?我找不到合适的答案。
我对RxJava/RxAndroid还不熟悉,但我一直坚持使用我的用例。 我尝试迭代一个
我有3个EditText字段,我为这些字段创建了3个可观察对象。 当这三个字段都有值时,我想启用一个按钮。用户可以在字段中按任意顺序输入值。我该怎么做? 我用zip实现了这一点。 当我在所有三个文本字段中输入某个内容时,上面的这种情况就会起作用。e、 g.我在所有三个文本字段中输入了1个字符,然后按钮将被启用。但当我删除这三个字段中的任何一个字符时。zip不会被调用,因为它将等待其他2个文本字段流
我只是从改型和RXJava开始。我有一个api调用,它上传一个文件并返回一个http响应,该响应的主体为空,状态为200(以防everythign工作正常)。我把它翻译成: 第二步,在我弄清楚如何映射到改型的请求是链多文件上传。我该怎么做? 也欢迎链接到使用RXJava+Retrifit的高级教程或示例。