小编Mat*_*Bos的帖子

RxJava2/Retrofit2 - 为204 PUT和DELETE请求处理null

因此,我正在使用一个明确定义的API,旨在不返回有效负载主体DELETEPUT操作.

这在Rx 0.X和Rx 1.x中是可接受的.现在我正在更新到Rx 2并且存在一个存在的危机,我应该如何处理空值.内容长度和正文当然是null,导致:

java.lang.NullPointerException: Null is not a valid element
   at io.reactivex.internal.queue.SpscLinkedArrayQueue.offer(SpscLinkedArrayQueue.java:68)
   at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.onNext(ObservableObserveOn.java:116)
   at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:63)
Run Code Online (Sandbox Code Playgroud)

在doOnNext中.

我见过很多人的建议Optional<>但是我也需要支持Java7以用于用例原因.我尝试了后端移植,但我无法让它工作.我也不想膨胀并为他们的版本导入Guava库.

我也注意到flatMap也可以帮我处理这个与地图相反的问题,而我正在阅读这些差异.

目前我有一个非常粗糙的OkHttp3拦截器,它将检查状态,检查有效负载是否为空,并添加错误的虚拟内容.

我也试过添加转换工厂.

任何人都可以提供建议并指导我正确的道路是什么?当然,API可以改变,但204不应该因为它的定义为HTTP状态代码而具有有效载荷.

相关依赖

compile('com.squareup.retrofit2:retrofit:2.1.0') {
     exclude module: 'okhttp'
} 
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

compile 'io.reactivex.rxjava2:rxjava:2.0.5'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.trello.rxlifecycle2:rxlifecycle:2.0.1'
compile 'com.trello.rxlifecycle2:rxlifecycle-components:2.0.1'
Run Code Online (Sandbox Code Playgroud)

rx-java retrofit2 okhttp3

12
推荐指数
2
解决办法
5223
查看次数

Activity和Fragment中的自动UI配置更改处理有时会失败

我已经写了很长时间的Android应用程序,但现在我遇到了一个我从未想过的问题.这是关于Android的生命周期Activitys,并Fragments在有关的配置更改.为此,我用这个必要的代码创建了一个小应用程序:

public class MainActivity extends FragmentActivity {

    private final String TAG = "TestFragment";
    private TestFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getSupportFragmentManager();
        fragment = (TestFragment) fm.findFragmentByTag(TAG);

        if (fragment == null) {
            fragment = new TestFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment, TAG).commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码TestFragment.需要注意的是我打电话setRetainInstance(true);onCreate方法,这样的片段没有在配置更改后recrated.

public class TestFragment extends Fragment implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater li, …
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle android-fragments android-activity

8
推荐指数
1
解决办法
783
查看次数

REGEXP:捕获组后面没有

我需要匹配以下语句:

Hi there John
Hi there John Doe (jdo)
Run Code Online (Sandbox Code Playgroud)

不匹配这些:

Hi there John Doe is here 
Hi there John is here
Run Code Online (Sandbox Code Playgroud)

所以我认为这个正则表达式会起作用:

^Hi there (.*)(?! is here)$
Run Code Online (Sandbox Code Playgroud)

但它没有 - 我不知道为什么 - 我相信这可能是由捕获组 (.*) 引起的,所以我认为让 * 操作员变得懒惰可能会解决问题......但不是。这个正则表达式也不起作用:

^Hi there (.*?)(?! is here)$
Run Code Online (Sandbox Code Playgroud)

任何人都可以指出我的解决方案方向吗?

解决方案

要检索没有 is here结尾的句子(如Hi there John Doe (the second)),您应该使用(作者@Thorbear):

^Hi there (.*$)(?<! is here)
Run Code Online (Sandbox Code Playgroud)

对于中间包含一些数据的句子(例如 Hi there John Doe (the second) is hereJohn Doe(第二个)是所需的数据)简单的分组就足够了:

^Hi there (.*?) is here$
Run Code Online (Sandbox Code Playgroud)

.

           ???????????????????????????????????????????? …
Run Code Online (Sandbox Code Playgroud)

java regex negative-lookahead capturing-group

5
推荐指数
1
解决办法
2439
查看次数