问题描述
在尝试使用okhttp进行API请求后,我试图将变量从一个类传递到另一个类.
I´m trying to get variables from one Class to another after an API request with okhttp.
API请求(在我的MainActivity中):(具有普通类 Class MainActivity:AppCompatActivity(){
)
API Request (in my MainActivity): (with the normal class class MainActivity : AppCompatActivity(){
)
fun fetchJson() {
val url = "https://......./api/.../....."
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback{
override fun onResponse(call: Call, response: Response) {
val body = response?.body?.string()
println(body)
val gson = GsonBuilder().create()
val ApiStats = gson.fromJson(body, ApiStats::class.java)
}
override fun onFailure(call: Call, e: IOException) {
println("API execute failed")
}
})
}
结果将在额外的班级中"保存":
And the result will be "saved" in a extra Class:
class ApiStats(val apple: String)
现在我想在前面已经提到的 MainActivity 类中获取val
And now I want to get the val in my MainActivity class which already mentioned before
我已经很有趣地看到了可能性,但是它似乎对我没有用...
I have already seen the possibility with a fun but it didn't seems to work for me...
我也尝试过: Class MainActivity(val apiStats:ApiStaats):AppCompatActivity()
但是当我启动应用程序时,它会使进程崩溃
But when I start the app it crashes the process
2020-02-25 13:36:53.466 10543-10543/example.spectre.vision E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.spectre.vision, PID: 10543
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{example.spectre.vision/example.spectre.vision.MainActivity}: java.lang.InstantiationException: java.lang.Class<example.spectre.vision.MainActivity> has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.InstantiationException: java.lang.Class<example.spectre.vision.MainActivity> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
```
推荐答案
不能将属性放入Activity构造函数的原因是Android要求所有Activity在构造函数中都没有参数.这不是典型的Java或Kotlin要求,但Android使用空的构造函数通过反射创建Activity的实例.您可以像这样将属性放在类的主体中:
The reason you can't put the property in the Activity constructor is that Android requires all Activities to have no arguments in the constructor. This is not a typical Java or Kotlin requirement, but Android uses the empty constructor to create instances of your Activity via reflection. You can put the property in the body of the class like this:
class MyActivity: AppCompatActivity() {
var apiStats: ApiStats? = null
//...
}
然后就是从另一个对象获取数据的问题.替换此行:
Then there's the matter of getting that data back from your other object. Replace this line:
val ApiStats = gson.fromJson(body, ApiStats::class.java)
使用
apiStats = gson.fromJson(body, ApiStats::class.java)
您可能希望在此行后面加上更新UI或其他内容的其他代码.
You will probably want to follow that line with other code that updates the UI or something.
这篇关于Kotlin从一类到另一类获取变量/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!