cannot execute Google Cloud Functions Android Client Call.
I create Cloud Function from cloud console, and test works on cloud console.
but on Android client app, it's not work.
so, i did import Google Cloud Service Quick Start Project on my android studio, same error too.
this is quick start project url.
https://cloud.google.com/functions/docs/quickstart-console
this is my code.
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;
}
});
}
}
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)
all code reffered by Firebase offical site https://firebase.google.com/docs/functions/callable?authuser=0
on Cloud Console DashBoard , API Traffics catchs
what is matter?
Response at a minimum must include a JSON object with data or error values.
If you modify your function to be:
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});
}
};
You should be ok.
check https://firebase.google.com/docs/functions/callable-reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With