我想使用rxjava并进行改造。 这是我的改造建设者:
Retrofit provideRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(UrlManager.API_HOST)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
和okHttpClient:
public OkHttpClient client(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(loggingInterceptor)
.connectTimeout(10, TimeUnit.SECONDS)
.build();
}
在我的登录活动中,我插入了我的用户名和密码,当点击loginButton时,此方法被调用:
public DisposableObserver observeLoginButton() {
return view.observeLoginBtn()
.subscribeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
view.loadButtonAnimation(); //load animation
}
})
.map(new Function<Object, String>() {
@Override
public String apply(Object o) throws Exception {
return view.getUserAndPassword(); // get userName and password as a string from edittext
}
})
.switchMap(new Function<String, Observable<String>>() {
@Override
public Observable<String> apply(String s) throws Exception {
String[] info = s.split(" "); // split user name and pass
return model.getLoginCookie(info[0],info[1]); // send userName and pass to model and call my service
}
})
.observeOn(Schedulers.io())
.subscribeWith(view.observer());
}
对于测试我已将自己的登录信息插入发送服务。这是我的getLoginCookie方法:
public Observable<String> getLoginCookie(String userName, String password) {
Map<String, Object> obj = new ArrayMap<>();
obj.put("username", "JuJzWgbsDJ0lUlFYVzoxWg");
obj.put("password", "DD0vCYmzJuIPff9iKUpfQA");
obj.put("customCredential", "6298f927-f98a-44eb-a312-780674a76245,Mobile89954324581380882887");
obj.put("isPersistent", false);
RequestBody body = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"),
(new JSONObject(obj)).toString());
return service.getAuthentication(body);
}
我的服务工作为json parametr.So,以便我使用RequestBody将我的地图转换为json。然后我打电话给我的服务:
@POST(UrlManager.AUTHENTICATION+"Login")
Observable<String> getAuthentication(@Body RequestBody params);
当我运行我的应用程序并单击登录按钮时,我得到了这个:
D/Payesh: --> POST http://****/Authentication.svc/json/Login (183-byte body)
W/System.err: remove failed: ENOENT (No such file or directory) : /data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp
D/Payesh: <-- HTTP FAILED: android.os.NetworkOnMainThreadException
I/----->: apply: null
1 - 为什么我得到了android.os.NetworkOnMainThreadException?
2 - 我如何追踪我的参数是否正确发送到我的网络服务并建立连接?我在java.lang.NullPointerException: The supplied value is null
观察员身上得到了onError(Throwable e)
。
答案 0 :(得分:1)
为什么我得到android.os.NetworkOnMainThreadException?
您正在订阅note_ids=($(listnotes | sed 's/^\(.*\) \(.*\)$/\1 "\2"/'))
并观看AndroidSchedulers.mainThread()
。这意味着您正在主线程上进行网络请求。通过在Schedulers.io()
之前添加.observeOn(Schedulers.io())
,在网络请求之前切换主题。如果你在完成任务后做任何与视图相关的任务,最后请观察主线程。
switchMap
对于
return view.observeLoginBtn()
.subscribeOn(AndroidSchedulers.mainThread())
.doOnNext(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
view.loadButtonAnimation(); //load animation
}
})
.map(new Function<Object, String>() {
@Override
public String apply(Object o) throws Exception {
return view.getUserAndPassword(); // get userName and password as a string from edittext
}
})
.observeOn(Schedulers.io())
.switchMap(new Function<String, Observable<String>>() {
@Override
public Observable<String> apply(String s) throws Exception {
String[] info = s.split(" "); // split user name and pass
return model.getLoginCookie(info[0],info[1]); // send userName and pass to model and call my service
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(view.observer());
确保hoy已成功为缓存创建了FIle。
我如何追踪我的参数是否正确发送到我的网站 服务和连接是否成立?
如果您使用了 remove failed: ENOENT (No such file or directory) : /data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp
,则会打印有关您的请求的所有信息,例如HttpLoggingInterceptor
,Header
,Body
。