完成 Firebase 函数(Uri 和字符串)时出现 ClassCastException Android Java
ClassCastException while completing Firebase function (Uri and String) Android Java
我想制作一个 Task 方法来调用 Cloud 函数(firebase 的东西)并在 Android 应用程序中完成 onCompleteListenner
应用程序时的代码抛出 ClassCastException 有点 "String cannot be cast to Uri"。我该怎么办?
这是需要的代码:
private Task<Uri> getProfilePicture(){
return functions
.getHttpsCallable("getProfilePicture")
.call()
.continueWith(task -> (Uri) task.getResult().getData())
.addOnCompleteListener(task -> {
String pfp = task.getResult().toString();
Uri pfp2 = Uri.parse(pfp);
setProfilePhoto(pfp2);
});
}
函数代码:
import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
admin.initializeApp()
export const getProfilePicture = functions.https.onRequest((request, response) => {
const promise = admin.storage().bucket().file('usersPfp/aomkefQvkRQCuB66m6L9m94k9Bf1').getSignedUrl({
action: 'read',
expires: '03-09.2441'
})
const p2 = promise.then(GetSignedUrlResponse => {
const data = GetSignedUrlResponse[0]
return response.send({"data": data})
})
p2.catch(error =>{
console.log(error)
return response.status(500).send({"error": error})
})
})
还有一个例外:
2020-01-19 15:41:04.150 28029-28029/com.happs.medrate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.happs.medrate, PID: 28029
com.google.android.gms.tasks.RuntimeExecutionException: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture$AccountFragmentViewModel(AccountFragmentViewModel.java:76)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$OKnCSppMNZTxWBSaV67HE9UL6mo.onComplete(Unknown Source:2)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture[=14=](AccountFragmentViewModel.java:74)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$gUAaj3fCSegHGtCnpDov7VKk9aQ.then(Unknown Source:0)
at com.google.android.gms.tasks.zzd.run(Unknown Source:5)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
尝试continueWith(task -> Uri.parse(task.getResult().getData()))
。
之后就不需要在addOnCompleteListener
中解析了。
我想制作一个 Task 方法来调用 Cloud 函数(firebase 的东西)并在 Android 应用程序中完成 onCompleteListenner
应用程序时的代码抛出 ClassCastException 有点 "String cannot be cast to Uri"。我该怎么办?
这是需要的代码:
private Task<Uri> getProfilePicture(){
return functions
.getHttpsCallable("getProfilePicture")
.call()
.continueWith(task -> (Uri) task.getResult().getData())
.addOnCompleteListener(task -> {
String pfp = task.getResult().toString();
Uri pfp2 = Uri.parse(pfp);
setProfilePhoto(pfp2);
});
}
函数代码:
import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
admin.initializeApp()
export const getProfilePicture = functions.https.onRequest((request, response) => {
const promise = admin.storage().bucket().file('usersPfp/aomkefQvkRQCuB66m6L9m94k9Bf1').getSignedUrl({
action: 'read',
expires: '03-09.2441'
})
const p2 = promise.then(GetSignedUrlResponse => {
const data = GetSignedUrlResponse[0]
return response.send({"data": data})
})
p2.catch(error =>{
console.log(error)
return response.status(500).send({"error": error})
})
})
还有一个例外:
2020-01-19 15:41:04.150 28029-28029/com.happs.medrate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.happs.medrate, PID: 28029
com.google.android.gms.tasks.RuntimeExecutionException: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.google.android.gms.tasks.zzu.getResult(Unknown Source:15)
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture$AccountFragmentViewModel(AccountFragmentViewModel.java:76)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$OKnCSppMNZTxWBSaV67HE9UL6mo.onComplete(Unknown Source:2)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to android.net.Uri
at com.happs.medrate.viewmodel.main.AccountFragmentViewModel.lambda$getProfilePicture[=14=](AccountFragmentViewModel.java:74)
at com.happs.medrate.viewmodel.main.-$$Lambda$AccountFragmentViewModel$gUAaj3fCSegHGtCnpDov7VKk9aQ.then(Unknown Source:0)
at com.google.android.gms.tasks.zzd.run(Unknown Source:5)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
尝试continueWith(task -> Uri.parse(task.getResult().getData()))
。
之后就不需要在addOnCompleteListener
中解析了。