Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向服务发送JSON参数_Android_Retrofit2_Rx Android - Fatal编程技术网

Android 向服务发送JSON参数

Android 向服务发送JSON参数,android,retrofit2,rx-android,Android,Retrofit2,Rx Android,我想使用rxjava并进行改造。 这是我的改装生成器: Retrofit provideRetrofit(OkHttpClient okHttpClient) { return new Retrofit.Builder() .client(okHttpClient) .baseUrl(UrlManager.API_HOST) .addConverterFactory(GsonConverterFactory.crea

我想使用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());
    }
public-DisposableObserver-observeLoginButton(){
返回视图.observeLoginBtn()
.subscribeOn(AndroidSchedulers.mainThread())
.doOnNext(新消费者){
@凌驾
公共void接受(对象o)引发异常{
view.loadButtonImation();//加载动画
}
})
.map(新函数(){
@凌驾
公共字符串应用(对象o)引发异常{
return view.getUserAndPassword();//从edittext获取用户名和密码作为字符串
}
})
.switchMap(新函数(){
@凌驾
公共可观察应用(字符串s)引发异常{
String[]info=s.split(“”;//拆分用户名和密码
返回model.getLoginCookie(信息[0],信息[1]);//发送用户名并传递给model,然后调用我的服务
}
})
.observeOn(Schedulers.io())
.subscribowith(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);
}
public observeable getLoginCookie(字符串用户名、字符串密码){
Map obj=新数组映射();
obj.put(“用户名”,“JuJzWgbsDJ0lUlFYVzoxWg”);
对象put(“密码”,“DD0vCYmzJuIPff9iKUpfQA”);
obj.put(“客户凭证”,“6298f927-f98a-44eb-a312-780674a76245,手机89954324581380882887”);
obj.put(“Ispersist”,假);
RequestBody=RequestBody.create(
parse(“application/json;charset=utf-8”),
(新的JSONObject(obj)).toString();
返回服务.getAuthentication(body);
}
我的服务作为一个json参数工作。因此,为了实现这一点,我使用RequestBody将我的映射转换为json。然后我给我的服务台打了电话:

@POST(UrlManager.AUTHENTICATION+"Login")
Observable<String> getAuthentication(@Body RequestBody params);
@POST(UrlManager.AUTHENTICATION+“Login”)
可观察的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
D/Payesh:-->POST http://**/Authentication.svc/json/Login(183字节正文)
W/System.err:remove失败:enoint(没有这样的文件或目录):/data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp
D/Payesh::应用:空
1-为什么我得到了android.os.NetworkOnMainThreadException

2-如何跟踪我的参数是否正确并发送到我的web服务,以及是否建立了连接?我刚刚得到了
java.lang.NullPointerException:observer中的
onError(Throwable e)
上提供的值为null

为什么我得到了android.os.NetworkOnMainThreadException

您正在订阅
AndroidSchedulers.mainThread()
并在
Schedulers.io()上观察。这意味着您正在主线程上执行网络请求在网络请求之前通过添加
.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());
确保hoy已成功创建缓存文件

如何跟踪我的参数是否正确并发送到我的web 服务和连接是否已建立


由于您使用了
HttpLoggingInterceptor
,这将打印有关请求的所有信息,如
标题
正文
响应

您在查看.observeLoginBtn()
line中实际在做什么?@AbuYousuf什么都没有。它只是由Rx实现的按钮侦听器。@AbuYousuf
public observeLoginBtn(){return RxView.clicks(mBinding.buttonLogin);}
请发布与单击相关的帖子event@AbuYousuf我确实寄了。查看顶部帖子。
public observeLoginBtn(){return RxView.clicks(mBinding.buttonLogin);}
当我添加
subscribeOn(Schedulers.io())
和你一样,当我单击我的按钮时,我的应用程序崩溃了
java.lang.IllegalStateException:找不到方法加载(视图)在活动类com.groot.payesh.activities.login.LoginActivity for onClick handler中,id为“buttonLogin”的视图类android.widget.FrameLayout
我使用了
HttpLoggingInterceptor
,但没有显示我上面提到的
view
这是什么实例?你必须发布所有相关代码。请编辑您的问题并发布所有相关代码?我使用的是MVP架构。视图是使我的登录布局膨胀的布局。请立即编辑答案检查
 remove failed: ENOENT (No such file or directory) : /data/data/com.groot.payesh/cache/okhttp_cache/journal.tmp