我想通过改型添加自签名证书,因为我需要传递上下文,但是在通过MVC模型传递上下文时会遇到一些问题
改造客户
object RetrofitClient {
fun call(context: Context): Webservice {
val webservice by lazy {
Retrofit.Builder()
// .baseUrl("http://68.183.183.255:8080")
//.baseUrl("http://my-json-server.typicode.com")
.baseUrl("https://54roh005p5.execute-api.us-west-2.amazonaws.com/mdv/cloud/login/")
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addConverterFactory(ScalarsConverterFactory.create())
.client(SelfSigningClientBuilder.createClient(context))
.build().create(Webservice::class.java)
}
return webservice
}
}
存储库
class TodoRepository(context:Context) {
private var client:Webservice = RetrofitClient.call(context)
//suspend fun getTodo(id: String) = client.getTodo(id)
suspend fun getTodo(id: User) = client.getTodo(id)
}
ViewModel
class NetworkViewModel(context: Context) : ViewModel() {
var repository: TodoRepository = TodoRepository(context)
fun getTodo(arg : User):LiveData<Response<com.example.myapplication.entity.UserData>> {
val result = liveData(Dispatchers.IO) {
val retrivedTodo = repository.getTodo(arg)
emit(retrivedTodo)
}
return result
}
}
MainActivity
networkViewModel=ViewModelProviders.of(this).get(NetworkViewModel(this)::class.java)
出现错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.myapplication.network.NetworkViewModel
我在做什么错。如果有更好的方法,请建议
答案 0 :(得分:0)
不要通过构造函数初始化ViewModel对象:-NetworkViewModel(this)::class.java
是错误的。使用:-
networkViewModel = ViewModelProviders.of(this).get(NetworkViewModel::class.java)
答案 1 :(得分:0)
您可以自定义您的OkHttpClients sslSocketFactory
例如({Retrofit
的客户端设置)
val retrofit Retrofit
.Builder()
.client(client)
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
例如(OkHttpsClient
的sslSocketFactory设置)
val client =OkHttpClient.Builder()
.sslSocketFactory(sslContextWithSS.getSocketFactory())
.build()
例如(带有自定义证书的SSLSocketFactory
)
val keyStore = readKeyStore() //your method to obtain KeyStore
val sslContext = SSLContext.getInstance("SSL")
val trustManagerFactory = TrustManagerFactory.getInstance(getDefaultAlgorithm())
trustManagerFactory.init(keyStore)
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
keyManagerFactory.init(keyStore, "keystore_pass".toCharArray())
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), SecureRandom())
client.setSslSocketFactory(sslContext.getSocketFactory())
通过KeyStore
获得CustomCertificate
fun readKeyStore()= KeyStore.getInstance(KeyStore.getDefaultType()).also{
try (val inputStream = context.getResources().openRawResource(R.raw.custom_certificate)) {
it.load(inputStream, getPassword());
}
}
答案 2 :(得分:0)
宁可在类之间传递上下文,最好创建一个应用程序类并在需要时使用此应用程序上下文。请检查以下内容:
<ReactPlayer
url='https://www.youtube.com/watch?v=mFJZ0HaYYB8'
config={{
youtube: {
playerVars: { modestbranding: 1 }
}
}}
/>
然后,在class MainApplication : Application() {
companion object {
private lateinit var instance: MainApplication
fun applicationContext() : Context {
return instance.applicationContext
}
}
override fun onCreate() {
super.onCreate()
instance = this
}
}
的application标签中添加此AndroidManifest
然后调用MainApplication.applicationContext()从任何地方获取Context。