代码之家  ›  专栏  ›  技术社区  ›  yozhik

Retrotif2+RxJava发送POST请求失败

  •  0
  • yozhik  · 技术社区  · 6 年前

    我想用reformation+RxJava发送POST请求,但是失败了,我不知道原因。在一个活动中它起作用,在另一个活动中-不想起作用:

    private void sendMerchantInfo() {
            try {
                String advertiserOriginalDeepLink = "https://mywebsite.com/main-1?param1=value1&param2=value2";
                    String urlGetParams = LinkParser.getUrlGETParams(advertiserOriginalDeepLink);
    
                    Map<Object, Object> merchantInfo = LinkParser.parseUrlGetParams(urlGetParams);
                    String merchantInfoJson = new Gson().toJson(merchantInfo); //{"param1":"value1","param2":"value2"}
    
                    String url = "https://api.endpoint.com/v1/system/merchant/process";
                    userService = this.serviceGenerator.createService(UserService.class, true);
                    final Observable observable = userService.sendUserInfo(
                            url, new RetrofitMapBody(merchantInfo))
                            .doOnNext(new Consumer<ResponseBody>() {
                                @Override
                                public void accept(ResponseBody responseBody) throws Exception {
                                    //handle 200 OK.
                                }
                            })
                            .onErrorResumeNext((ObservableSource<? extends ResponseBody>) v ->
                                    Crashlytics.log("Send user info attempt failed."))
                            .subscribeOn(Schedulers.from(threadExecutor))
                            .observeOn(postExecutionThread.getScheduler());
                    addDisposable(observable.subscribe());
                }
            } catch (Exception exception) {
                Crashlytics.log("Send user info attempt failed. " + exception.getMessage());
            }
        }
    

    我怀疑这部分有问题,我正试着提出请求 OnCreate() 方法:

    .subscribeOn(Schedulers.from(threadExecutor))
    .observeOn(postExecutionThread.getScheduler());
    

    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
    

    onErrorResumeNext() 可能是线程的问题,因为有一次我遇到异常: networkonmainthreadexception

    1 回复  |  直到 6 年前
        1
  •  2
  •   Muhammad Youssef    6 年前

    RxJava2 Adapter ,它会救你很多!

       private Retrofit getRetrofitClient() {
            return new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) //option 1
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.newThread())) //option 2
                    .build();
        }
    

    步骤2: APIService接口(示例)

    @GET("endpoint")
    Single<ResponseModel> fetch();
    

    步骤3: 用法

      Single<ResponseModel> fetch() {
            return getRetrofitClient()
                        .create(APIService.class)
                        .fetch();
        }
    
    • HttpException 从中可以提取状态代码、状态消息和完整的HTTP响应。

    • 任何连接错误都将包含在 IOException