java - Firebase函数模拟器不适用于android
问题描述
我已经尝试了 3 天的不同解决方案,无法弄清楚。这是我的代码:
FirebaseLogic.java
public class FirebaseLogic {
// Access a Cloud Firestore instance from your Activity
static FirebaseFirestore db ;
//Firebase functions instance
static FirebaseFunctions mFunctions;
// Create a storage reference from our app
static FirebaseStorage storage ;
static StorageReference storageRef ;
public static void init()
{
//////////Remove this if you re not working with emulator anymore
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.useEmulator("10.0.2.2", 8080);
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build();
firestore.setFirestoreSettings(settings);
mFunctions = FirebaseFunctions.getInstance();
mFunctions.useEmulator("10.0.2.2.", 5001);
//////////////////////////////////////////////////////
// Access a Cloud Firestore instance from your Activity
db = FirebaseFirestore.getInstance();
// Create a storage reference from our app
storage = FirebaseStorage.getInstance();
storageRef = storage.getReference();
}
public static Task<String> addMessage(String text) {
// Create the arguments to the callable function.
Map<String, Object> data = new HashMap<>();
data.put("text", text);
data.put("push", true);
return mFunctions
.getHttpsCallable("addMessage")
.call(data)
.continueWith(new Continuation<HttpsCallableResult, String>() {
@Override
public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
// This continuation runs on either success or failure, but if the task
// has failed then getResult() will throw an Exception which will be
// propagated down.
String result = (String) task.getResult().getData();
Log.w("messi", result);
return result;
}
});
}
}
Activity.java(这就是我调用firebase函数只是为了测试的方式)
//...
Button button = (Button) root.findViewById(R.id.BtnAddImages);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FirebaseLogic.init();
FirebaseLogic.addMessage("hmida").addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
FirebaseFunctionsException.Code code = ffe.getCode();
Object details = ffe.getDetails();
}
Log.w("functions", "No Success");
}
if(task.isSuccessful())
{
Log.w("functions", "Success");
}
// ...
}
});
}
});
});
//...
index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.addMessage = functions.https.onCall((data, context) => {
return "hello";
});
顺便说一句,firestore 工作正常,我只有函数有问题,我已经在 gradle 文件中添加了实现,所以不是这样我试图记录异常消息,但他们没有提供任何有用的信息,他们只是说“内部的”
解决方案
推荐阅读
- android - 如何在 Shopify 中设置 CCAvenue redirect_url 或 cancel_url?
- javascript - 组织 JS 代码以获得最佳性能的最佳方法是什么?
- ios - Xcode 10 测试目标构建错误
- android - Xamarin Forms (Android) 在没有网络连接时退出应用程序
- python - 使用 pandas 数据框创建特定列的热向量
- three.js - 如何在 webvr 中嵌入 HTML 页面
- java - spring boot 无法使用 Intellij 连接到 MYSQL 数据库
- python - 使用空查询执行 twitter.search.tweets
- vue.js - Vue 自定义数字输入组件使用 0.01 步仅更新 int 值
- android - 如何在linphone android java代码中禁用UDP传输