Google Cloud Functions - 客户端调用错误(Android Studio):FirebaseFunctionsException:响应不是有效的 JSON 对象
Posted
技术标签:
【中文标题】Google Cloud Functions - 客户端调用错误(Android Studio):FirebaseFunctionsException:响应不是有效的 JSON 对象【英文标题】:Google Cloud Functions - Client Call Error (Android Studio) :FirebaseFunctionsException: Response is not valid JSON object 【发布时间】:2018-12-13 12:46:30 【问题描述】:无法执行 Google Cloud Functions android 客户端调用。
我从云控制台创建云函数,并在云控制台上测试工作。
但在 Android 客户端应用程序上,它不起作用。
所以,我确实在我的 android studio 上导入了谷歌云服务快速启动项目,同样的错误。
这是快速启动项目的网址。
https://cloud.google.com/functions/docs/quickstart-console
这是我的代码。
谷歌云函数调用类
public class TestClass
private FirebaseFunctions mFunctions;
public TestClass()
mFunctions=new FirebaseFunctions();
..
public Task<String> myFunc(String text)
// Create the arguments to the callable function, which is just one string
Map<String, Object> data = new HashMap<>();
data.put("message", text);
return mFunctions
.getHttpsCallable("myFunc")
.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.d("gcf",result);
return result;
);
在 MainActivity 执行方法中
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
TestClass testClass=new TestClass();
testClass.myFunc("im bhw")
.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();
// [START_EXCLUDE]
Log.w("gcf", "myFunc:onFailure", e);
Toast.makeText(getApplicationContext(),"An error occurred.",Toast.LENGTH_SHORT).show();
return;
// [END_EXCLUDE]
// [START_EXCLUDE]
String result = task.getResult();
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
);
);
谷歌云函数
exports.myFunc = (req, res) =>
if (req.body.message === undefined)
res.status(400).send('No message defined!');
else
console.log(req.body.message);
res.status(200).send('Success: ' + req.body.message);
;
exports.myFunc = (req, res) =>
if (req.body.message === undefined)
res.status(400).send('No message defined!');
else
console.log(req.body.message);
res.status(200).send('Success: ' + req.body.message);
;
结果
com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object.
at com.google.firebase.functions.FirebaseFunctions$4.onResponse(SourceFile:296)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.google.firebase.functions.FirebaseFunctions$4.onResponse(SourceFile:294)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761) com.google.firebase.functions.FirebaseFunctionsException: Response is not valid JSON object.
at com.google.firebase.functions.FirebaseFunctions$4.onResponse(SourceFile:296)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.google.firebase.functions.FirebaseFunctions$4.onResponse(SourceFile:294)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Firebase 官方网站引用的所有代码 https://firebase.google.com/docs/functions/callable?authuser=0
在 Cloud Console DashBoard 上,API 流量捕获
什么是问题?
【问题讨论】:
请出示您的 Cloud Functions 代码。 我添加了云功能。它是自动生成的代码。谢谢。 我发现 Google Cloud Service Project 和 Firebase Project 是不同的。所以我连接了两个项目并尝试了,然后错误代码被更改。但还是不行。 【参考方案1】:响应至少必须包含具有 data 或 error 值的 JSON 对象。
如果您将函数修改为:
exports.myFunc = (req, res) =>
if (req.body.message === undefined)
res.status(400).send(error: 'No message defined!');
else
console.log(req.body.message);
res.status(200).send(data: 'Success: ' + req.body.message);
;
exports.myFunc = (req, res) =>
if (req.body.message === undefined)
res.status(400).send(error: 'No message defined!');
else
console.log(req.body.message);
res.status(200).send(data: req.body.message);
;
你应该没事的。
查看https://firebase.google.com/docs/functions/callable-reference
【讨论】:
以上是关于Google Cloud Functions - 客户端调用错误(Android Studio):FirebaseFunctionsException:响应不是有效的 JSON 对象的主要内容,如果未能解决你的问题,请参考以下文章
? Google Cloud Functions ?????? MongoDB Atlas ??
如何从 Cloud Functions 连接 Google Cloud SQL?