我正在尝试创建 Espresso 测试,mockWebServer当我尝试创建mockWebServer它时,它会调用真正的 api 调用,并且我想拦截它并模拟响应。
我的匕首组织是:
我的应用程序
open class App : Application(), HasAndroidInjector {
lateinit var application: Application
@Inject
lateinit var androidInjector: DispatchingAndroidInjector<Any>
override fun androidInjector(): AndroidInjector<Any> = androidInjector
override fun onCreate() {
super.onCreate()
DaggerAppComponent.factory()
.create(this)
.inject(this)
this.application = this
}
}
Run Code Online (Sandbox Code Playgroud)
然后 MyAppComponent
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
AppModule::class,
RetrofitModule::class,
RoomModule::class,
AppFeaturesModule::class
]
)
interface AppComponent : AndroidInjector<App> {
@Component.Factory
interface Factory {
fun create(@BindsInstance application: App): AppComponent
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了这个 TestApp
class TestApp …Run Code Online (Sandbox Code Playgroud) 为了在 Android 中的本地主机上进行 http-API 调用,我需要添加一个网络安全配置来定义我的本地 IP 地址。当然,每个开发者的本地IP地址都是不同的,所以我把这个值放进去,gradle.properties这样每个人都可以自己覆盖它。但是,我还没有找到如何将该值包含在文件中network-security-config.xml。我尝试创建 abuildConfigField或resValuein build.gradle,但两者似乎都不起作用。我能做些什么?
网络安全配置.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<!-- We are allowed to call localhost on http instead of https:
/sf/ask/3215860301/ -->
<domain includeSubdomains="true">LOCALHOST</domain>
</domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
应用程序/build.gradle
buildTypes {
debug {
buildConfigField "String", "LOCALHOST", localIpAddress
resValue "string", "LOCALHOST", localIpAddress
}
}
Run Code Online (Sandbox Code Playgroud)
gradle.properties
localIpAddress="IP_ADDRESS_HERE"
Run Code Online (Sandbox Code Playgroud)
network_security_config.xml也欢迎任何其他如何优雅地包含我的本地 IP 地址的解决方案!
android localhost android-build-type android-network-security-config
我的应用程序使用 socket.io,无法连接到 node.js 服务器。
服务器节点.js
var app = require('http').createServer()
var io = require('socket.io')(app);
app.listen(1000);
io.on('connection', function (client) {
client.name = client.remoteAddress + ':' + client.remotePort;
console.log(client.name + ' connected!');
client.on('sensorChanged', function (data) {
console.log("HERE");
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
安卓应用:
SocketIO socket = new SocketIO();
try {
socket.connect("http://localhost:1000/", this);
txtView.setText("connected");
} catch (MalformedURLException e) {
e.printStackTrace();
}
socket.emit("sensorChanged", "argument1");
Run Code Online (Sandbox Code Playgroud)
当我连接到服务器时,服务器不会说“socket.name connected”,也不会发出事件“sensorChanged”。问题出在哪儿?
我只想在一个版本变体中信任证书.怎么做?
学习新技术是一个耗时的过程。它适用于带有 Java 的 Android。这又是针对带有 Swift 的 iOS 的。我现在面临着需要为 Android 和 iOS 应用程序提供服务器后端的挑战。但是,如果可能的话,我想避免学习一门新语言的大量时间投入。据我所知,服务器代码可以用 Java 或 Swift 编写,但 Swift 似乎是一种更简洁的语言,所以这就是我选择走这条路的原因。
我想制作一个概念验证示例,其中运行 Swift 的服务器能够与 Android 和 iOS 应用程序进行通信。应用程序发送请求并接收响应。
这个问题和我在下面的回答是我对服务器端 Swift 的介绍。
我的代码在android kitkat上工作,但是在Pie中运行它时出现io异常
不允许对server.com的明文HTTP流量
我正在使用凌空拨打服务器。
我已阅读Android 8: Cleartext HTTP traffic not allowed,但似乎没有任何答案允许我做我想做的事。
我有一个 Android 应用程序,其中另一个客户端可以使用证书来标识自己。应用程序想要验证该证书。验证证书的一部分是从证书颁发者处获取证书吊销列表 (CRL)。CRL 的分发点在证书中列出,并且不可避免地是一个 HTTP URL(CRL 本身由颁发者签名,因此不存在安全问题,如果它是 HTTPS URL,人们会想要验证保护 CRL 分发点的证书,并检查它是否已被吊销...)
可能的解决方案,以及为什么它们对我不起作用:
network_security_config.xml列出允许 HTTP 访问的域。遗憾的是,我在构建应用程序时不知道 URL - 这取决于 CA 决定放入其证书的内容。android:usesCleartextTraffic="true"清单。这意味着任何流量都可以是 HTTP,如果可能的话,我宁愿避免这种情况。(例如,与服务器的通信绝对必须是HTTPS,如果我不小心使用HTTP,我会出错。)代码有什么办法可以说“允许此连接为 HTTP”(但仅默认为 HTTPS)?
我们针对 Android 9 的 UI 测试失败,因为我们的测试环境不使用 HTTPS。问题是我们正在从 espresso 测试进行网络调用(例如,直接在后端创建用于测试的全新用户),这意味着它们在 espresso 测试应用程序上运行。
将 networkSecurityConfig 添加到应用程序清单无济于事,因为应用程序不是问题,而是 espresso 应用程序。
我怎样才能解决这个问题?浓缩咖啡应用程序是否有清单,我可以在其中添加该清单或以任何方式以编程方式更改此权限?
我的问题与
Retrofit和Android 8不支持 CLEARTEXT 通信:不允许明文 HTTP 流量
因为我找不到访问 espresso 应用程序清单的方法。我可以在我的应用程序清单中进行更改,但这并不重要,因为该应用程序已经在运行。我在浓缩咖啡部分失败了。
我有一个带有 react-native-firebase 通知的应用程序,消息已成功接收,通知已成功创建并显示在 Android Nougat 或更旧版本、Android Oreo 或更高版本上我添加了 channelId 它在使用 react-native run 时运行良好- android,但是当使用 bundle (cd android && ./gradlew installRelease) 时,我的通知不仅仅在 Android Oreo 或更高版本的前台显示,在后台工作。不知道问题出在消息侦听器上还是在创建通知时。这是我的代码...
我在获得权限后调用下面的函数,当应用程序在前台时收听消息
export const listening_message = () => {
return firebase.notifications().onNotification((notification) => {
display_notification({
notification_param: {
sound: 'default',
show_in_foreground: true,
},
title: notification.title,
subtitle: notification.subtitle,
body: notification.body,
data: notification.data,
});
});
}
Run Code Online (Sandbox Code Playgroud)
那么这是显示通知的代码
async function display_notification(parameter) {
let realmUser = await Realm.open(RealmData.User);
if(realmUser.objects(RealmDataName.User).length > 0) {
let realmNotification = await Realm.open(RealmData.NotificationID);
if(realmNotification.objects(RealmDataName.NotificationID).length == 0) {
realmNotification.write(() => …Run Code Online (Sandbox Code Playgroud) javascript android-notifications firebase react-native react-native-firebase
我正在尝试向 http 服务器发出 post 请求,但是当我尝试获取输入流时出现错误 java.io.IOException: Cleartext HTTP traffic to x not permitted
我已经尝试放入android:usesCleartextTraffic="true"我的清单,以及进行网络安全配置并将 android:targetSandboxVersion 设置为 1
app/src/main/res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">Server adress</domain>
</domain-config>
</network-security-config>
Run Code Online (Sandbox Code Playgroud)
应用程序/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.packagename"
android:targetSandboxVersion="1">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
Logcat 输出
D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
W/System.err: java.io.IOException: Cleartext HTTP traffic …Run Code Online (Sandbox Code Playgroud) android ×8
android-network-security-config ×1
certificate ×1
dagger ×1
dagger-2 ×1
firebase ×1
ios ×1
java ×1
javascript ×1
kotlin ×1
localhost ×1
node.js ×1
react-native ×1
socket.io ×1
swift ×1