ClassCastException 同时完成 Firebase 函数(Uri 和 String)Android Jav



我想创建一个调用云函数(firebase的东西(的Task方法,并且在完成onCompleteListenner应用程序时在Android应用程序的代码中抛出一个ClassCastException,有点"字符串不能被强制转换为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$1$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$0(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中解析它。

相关内容

  • 没有找到相关文章

最新更新