小编cod*_*ner的帖子

RxAndroid,Retrofit 2单元测试Schedulers.io

我刚学会了RxAndroid,但不幸的是我研究过的这本书没有涵盖任何单元测试.我在谷歌上搜索了很多,但没有找到任何简单的教程,以精确的方式涵盖RxAndroid单元测试.

我基本上使用RxAndroid和Retrofit 2编写了一个小的REST API.这是ApiManager类:

public class MyAPIManager {
    private final MyService myService;

    public MyAPIManager() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder b = new OkHttpClient.Builder();
        b.readTimeout(35000, TimeUnit.MILLISECONDS);
        b.connectTimeout(35000, TimeUnit.MILLISECONDS);
        b.addInterceptor(logging);
        OkHttpClient client = b.build();

        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://192.168.1.7:8000")
                .client(client)
                .build();

        myService = retrofit.create(MyService.class);
    }

    public Observable<Token> getToken(String username, String password) {
        return myService.getToken(username, password)
                .subscribeOn(Schedulers.io());
                .observeOn(AndroidSchedulers.mainThread());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建一个单元测试getToken.这是我的样本测试:

public class MyAPIManagerTest {
    private MyAPIManager myAPIManager;
    @Test …
Run Code Online (Sandbox Code Playgroud)

android unit-testing rx-android retrofit2 rx-java2

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

以编程方式从xib加载自定义UIView

我已经创建了一个自定义UIView MySample.xib.我已经将类添加MyView到了File Ownerxib中.

MyView.swift

class MyView: UIView {

    @IBOutlet var view: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    func setup() {
        NSBundle.mainBundle().loadNibNamed("MySample", owner: self, options: nil)            
        self.addSubview(self.view)
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在MyView从这样的MyController文件加载它:

MyController.swift

class MyController: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)

        view.addSubview(MyView())

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

现在要显示这个视图,我用来跟随另一个控制器的代码UIButton:

presentViewController(MyController(), animated: …
Run Code Online (Sandbox Code Playgroud)

uiview ios swift

6
推荐指数
1
解决办法
2万
查看次数

Android Studio 3.1默认或静态接口方法,不使用--min-sdk-version> = 24

我最近将Android Studio更新为3.1,我开始收到此错误:

默认接口方法仅从Android N开始支持(--min-api 24):void android.arch.lifecycle.DefaultLifecycleObserver.a(android.arch.lifecycle.h)消息{kind = ERROR,text =默认接口方法是仅支持从Android N(--min-api 24)开始:void android.arch.lifecycle.DefaultLifecycleObserver.a(android.arch.lifecycle.h),sources = [未知源文件],工具名称= Optional.of( D8)}

这是我的app build.gradle文件内容:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 27
    defaultConfig {
        applicationId "com.sample"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 11
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled true
    }
Run Code Online (Sandbox Code Playgroud)

我还使用以下Android架构组件:

implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:common-java8:1.1.1"
implementation "android.arch.persistence.room:runtime:1.0.0"
implementation "android.arch.persistence.room:rxjava2:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
Run Code Online (Sandbox Code Playgroud)

除此之外,我还使用Gradle构建工具版本3.1.0和Gradle版本4.4.

在对类似问题进行了一些搜索后,我也在gradle.properties 没有运气的项目中尝试了这个:

android.enableD8=true
Run Code Online (Sandbox Code Playgroud)

在Android Studio 3.0中一切正常,但是一旦我升级到3.1,我就开始收到此错误.

android android-studio android-architecture-components

6
推荐指数
1
解决办法
2250
查看次数

字体大小更改时动态调整容器大小

我在一个句子中单独显示每个字div使用inline-blockmax-width120px.当我尝试增加父div上的字体大小时,由于字体较大,div的内联块会重叠.

有没有办法以编程方式计算max-width增加字体大小后使用的div的内联块所需?

以下是示例代码段:

jQuery('.btnIncFont').click(function(){
  jQuery('.parentDiv').css('font-size',parseInt(jQuery('.parentDiv').css('font-size'))+2);
  });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btnIncFont">+</button>
<div class="parentDiv">
  <div style="display:inline-block;max-width:120px">This is a test1</div>
  <div style="display:inline-block;max-width:120px">This is a test2</div>
  <div style="display:inline-block;max-width:120px">This is a test3</div>
  <div style="display:inline-block;max-width:120px">This is a test4</div>
</div>
Run Code Online (Sandbox Code Playgroud)

按住+按钮,在某个阶段你会发现div相互重叠.我希望通过计算来解决这个问题,以便在增加font-size后max-width根据初始大小获得确切的比例120px.

javascript css jquery

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

iOS 模拟器“正在播放”屏幕丢失

几个月前我为 iOS 9+ 设备开发了我的应用程序,当时MPNowPlayingInfoCenter按预期工作。但最近我也将我的 XCode 更新到最新的 9.3,并且由于一些 Pods 库正在更新,我不得不将其更改Deployment Target为 10.0.0。从那以后MPNowPlayingInfoCenter停止工作,再也不会出现在任何模拟器设备的锁定屏幕上。

MPNowPlayingInfoCenter.default().nowPlayingInfo = [
            MPMediaItemPropertyTitle: self.playerItem.title.br2Sp.stripTags,
            MPMediaItemPropertyArtist: self.playerItem.artist.br2Sp.stripTags,
            MPNowPlayingInfoPropertyPlaybackRate: player.rate
        ]
Run Code Online (Sandbox Code Playgroud)

知道从那以后发生了什么变化吗?

PS停止工作是指我上面的代码不再Now Playing在锁定屏幕上显示信息。

xcode ios swift

4
推荐指数
1
解决办法
1039
查看次数

使用 ProGuard 调试 Android 的模糊堆栈跟踪

我一直在尝试使用 ProGuard 映射文件来混淆我的 Android 应用程序堆栈跟踪。我也尝试过在 Android Studio 的-verbose配置文件中使用并添加这些行:proguard-rules.pro

-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
Run Code Online (Sandbox Code Playgroud)

Unknown Source但在使用时仍然没有出现行号retrace.sh -verbose

知道为什么行号没有出现吗?

android android-proguard

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

UIStackView 使用 equalCentering 分布

我以编程方式创建UIStackView并添加了 2 个视图,每个视图都有 2 个子视图。这是我的示例代码:

let sv = UIStackView()
sv.axis = .horizontal
sv.alignment = .center
sv.spacing = Config.Dimensions.horizontalSpacing
sv.distribution = .equalCentering
sv.translatesAutoresizingMaskIntoConstraints = false

let viewCountStudent = UIView()
viewCountStudent.addSubview(studentCount)
viewCountStudent.addSubview(labelStudent)
studentCount.topAnchor.constraint(equalTo: viewCountStudent.topAnchor).isActive = true
studentCount.leftAnchor.constraint(equalTo: viewCountStudent.leftAnchor).isActive = true
studentCount.bottomAnchor.constraint(equalTo: viewCountStudent.bottomAnchor).isActive = true
labelStudent.topAnchor.constraint(equalTo: viewCountStudent.topAnchor).isActive = true
labelStudent.leftAnchor.constraint(equalTo: studentCount.rightAnchor, constant: 8.0).isActive = true
labelStudent.rightAnchor.constraint(equalTo: viewCountStudent.rightAnchor).isActive = true
labelStudent.bottomAnchor.constraint(equalTo: viewCountStudent.bottomAnchor).isActive = true

let viewCountLesson = UIView()
viewCountLesson.addSubview(lessonCount)
viewCountLesson.addSubview(labelLesson)
lessonCount.leftAnchor.constraint(equalTo: viewCountLesson.leftAnchor).isActive = true
lessonCount.topAnchor.constraint(equalTo: viewCountLesson.topAnchor).isActive = true
lessonCount.bottomAnchor.constraint(equalTo: viewCountLesson.bottomAnchor).isActive = true …
Run Code Online (Sandbox Code Playgroud)

ios swift uistackview

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

perl中用于字符串的正则表达式

有没有人知道正则表达式根据以下条件验证字符串:

  • 名称长度可以在1到255个字符之间.
  • 允许的字符是a-z,A-Z,0-9,'_'(下划线),' - '(连字符)和'.' (期).

谢谢.

regex perl

0
推荐指数
1
解决办法
75
查看次数

具有多个控制器的单个代表

我遇到了一个情况,我想用一个委托注册2个不同的UIViewControllers,因为我的项目一次显示2个UIViewControllers.当我触发事件时,我希望两个控制器都得到通知,但不幸的是只有任何一个控制器才能接收到这两个事件.

这是示例代码:

@objc protocol DownloaderDelegate: class {
    func complete()
}

class Downloader {
    static let sharedInstance = Downloader()
    weak var delegate: DownloaderDelegate?

    private init() {

    }

    func downloadFile() {
         self.delegate!.complete()
    }
}
Run Code Online (Sandbox Code Playgroud)

我在UIViewControllers中使用它就像这样:

override viewDidLoad() {
    super.viewDidLoad()

    Downloader.sharedInstance.delegate = self
}
Run Code Online (Sandbox Code Playgroud)

知道如何让视图控制器从单个委托中侦听事件吗?

delegates ios swift swift-protocols

0
推荐指数
1
解决办法
2353
查看次数