所以我目前正在处理Xamarin项目和App.Android项目,尝试连接Firebase Firestore时遇到困难。
public class FirestoreService : IFirestore, IOnSuccessListener
{
JavaDictionary<string, object> hash = new JavaDictionary<string, object> {{"test", 2}};
public IntPtr Handle => throw new NotImplementedException();
IntPtr IJavaObject.Handle => throw new NotImplementedException();
public async Task<string> PutTest(string UID)
{
try
{
DocumentReference docRef = FirebaseFirestore.Instance
.Collection("DB")
.Document(UID);
await docRef.Set(hash);
return "Sucess!";
}
catch(System.Exception e)
{
return e.ToString();
}
}
public async Task<string> RtvAllF(string UID)
{
try
{
CollectionReference docRef = FirebaseFirestore.Instance
.Collection("DB")
.Document(UID)
.Collection("Fach");
await docRef.Get().AddOnSuccessListener(this);
return "Success!";
}
catch (System.Exception e){throw new Exception(e.ToString());}
}
void IDisposable.Dispose(){throw new NotImplementedException();}
void IOnSuccessListener.OnSuccess(Java.Lang.Object result)
{
var snap = (QuerySnapshot)result;
if (!snap.IsEmpty)
{
var docs = snap.Documents;
foreach (DocumentSnapshot item in docs)
{
Debug.WriteLine(item);
}
}
}
}
调试:
12-27 15:23:23.247 W/me.MyAp(14029): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
12-27 15:23:23.397 W/me.MyAp(14029): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
12-27 15:23:23.398 W/me.MyAp(14029): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
12-27 15:23:23.402 W/me.MyAp(14029): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
12-27 15:23:23.402 W/me.MyAp(14029): Accessing hidden method Ljava/net/InetAddress;->isNumeric(Ljava/lang/String;)Z (light greylist, reflection)
12-27 15:23:23.403 W/me.MyAp(14029): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
12-27 15:23:23.461 W/me.MyAp(14029): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
12-27 15:23:23.498 W/me.MyAp(14029): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
12-27 15:23:23.498 W/me.MyAp(14029): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
**System.Exception:** 'System.InvalidCastException: Specified cast is not valid.
at Android.Gms.Tasks.TaskInvoker.AddOnSuccessListener (Android.Gms.Tasks.IOnSuccessListener p0) [0x0000c] in <b4d5e1ae9bbc49e2b688cfc4a2ef5009>:0
at MyApp.FirestoreService.RtvAllF (System.String UID) [0x00042] in C:\Users\%USER%\Source\Repos\%REPOS%\MyApp\MyApp\MyApp.Android\Code\FirestoreService.cs:57 '
12-27 15:23:33.298 W/Firestore(14029): (0.6.6-dev) [OnlineStateTracker]: Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds
12-27 15:23:33.298 W/Firestore(14029):
12-27 15:23:33.298 W/Firestore(14029): This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
由于我尝试从其他void写入数据库,因此我确定连接工作正常。但是我无法检索数据。我的Firestore安全规则是在“测试模式”中设置的。也不应该有任何问题。
所有NuGet软件包都以最新版本运行。
添加断点时,系统在AddOnSuccessListener处捕获异常:
System.Exception:**'System.InvalidCastException:指定的强制转换无效。 在Android.Gms.Tasks.TaskInvoker.AddOnSuccessListener(Android.Gms.Tasks.IOnSuccessListener p0)[0x0000c] in:0 在C:\ Users \ joshu \ Source \ Repos \%USER%\ MyApp \ MyApp \ MyApp.Android \ Code \ FirestoreService.cs:57
中的MyApp.FirestoreService.RtvAllF(System.String UID)[0x00042]