向服务发送 JSON 参数
Sending a JSON parameter to service
我想使用rxjava和retrofit。
这是我的改装建造者:
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();
}
在我的登录名 Activity 中,我输入我的用户名和密码,当单击登录按钮时,这个方法被调用:
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- 我如何跟踪我的参数是否正确发送到我的网络服务以及连接是否建立?我刚刚在观察者 onError(Throwable e)
上获得了 java.lang.NullPointerException: The supplied value is null
。
Why I got android.os.NetworkOnMainThreadException ?
您正在 AndroidSchedulers.mainThread()
订阅并在 Schedulers.io()
观察。这意味着您正在主线程上进行网络请求。 通过在 switchMap
之前添加 .observeOn(Schedulers.io())
在网络请求之前切换线程 。最后,如果您在完成后正在执行任何与视图相关的任务,请在主线程上观察。
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());
对于
remove failed: ENOENT (No such file or directory) : /data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp
确保霍伊已成功创建缓存文件。
How could i trace that my parameter is correct is send to my web
service and does connect is established or not?
因为您使用了 HttpLoggingInterceptor
,这将打印有关您的请求的所有信息,例如 Header
、Body
、Response
。
我想使用rxjava和retrofit。 这是我的改装建造者:
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();
}
在我的登录名 Activity 中,我输入我的用户名和密码,当单击登录按钮时,这个方法被调用:
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- 我如何跟踪我的参数是否正确发送到我的网络服务以及连接是否建立?我刚刚在观察者 onError(Throwable e)
上获得了 java.lang.NullPointerException: The supplied value is null
。
Why I got android.os.NetworkOnMainThreadException ?
您正在 AndroidSchedulers.mainThread()
订阅并在 Schedulers.io()
观察。这意味着您正在主线程上进行网络请求。 通过在 switchMap
之前添加 .observeOn(Schedulers.io())
在网络请求之前切换线程 。最后,如果您在完成后正在执行任何与视图相关的任务,请在主线程上观察。
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());
对于
remove failed: ENOENT (No such file or directory) : /data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp
确保霍伊已成功创建缓存文件。
How could i trace that my parameter is correct is send to my web service and does connect is established or not?
因为您使用了 HttpLoggingInterceptor
,这将打印有关您的请求的所有信息,例如 Header
、Body
、Response
。