
我在Android上使用Firebase Cloud Functions库,并使用gethttpsCallable调用云功能.
问题是该函数需要10-15秒才能将结果返回给客户端,因此客户端抛出异常java.net.socketTimeoutException:timeout.
码
// Create the arguments to the callable function. Map<String, Object> data = new HashMap<>(); data.put("info", info); mFunctions.gethttpsCallable(function) .call(data) .continueWith(new Continuation<httpsCallableResult, String>() { @OverrIDe public String then(@NonNull Task<httpsCallableResult> task) { // 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. if (task.isSuccessful()) { String result = (String) task.getResult().getData(); Log.v(Constants.LOG_TAG, result); return result; } else { // The condition never was true, always logs the exception. Exception e = task.getException(); Log.e(Constants.LOG_TAG, "Failed to join multiplayer room.", e); return null; } } });注意.我没有使用Okhttp,Retrofit或默认的系统网络功能,我正在使用Firebase云功能库(gethttpsCallable)来调用该功能.
解决方法:
我有同样的问题,所以我用Okhttp调用https函数而不是gethttpsCallable作为解决方法.
https.onCall的协议是公开的.
https://firebase.google.com/docs/functions/callable
使用Okhttp调用https函数的代码就在这里.
https://github.com/ryuta46/firebase-callable-okhttp/blob/56adc5e29a35bdb3b355c14d734e6145da4b6809/android/app/src/main/java/com/ttechsoft/okhttp_callable/MainActivity.kt#L184-L239
EditIEd.
重要部分的代码如下.
private fun callWithOkhttp(functionname: String) { val IDToken = IDToken ?: return val instanceID = instanceID ?: return val projectID = FirebaseApp.getInstance()?.options?.projectID ?: return val url = "https://us-central1-$projectID.cloudfunctions.net/$functionname" val JsonData = JsONObject() JsonData.put("text", "inputText") val Json = JsONObject() Json.put("data", JsonData) val requestbody = Requestbody.create(JsON, Json.toString()) val request = Request.Builder() .url(url) .post(requestbody) .addheader("Authorization", "Bearer $IDToken") .addheader("Firebase-Instance-ID-Token", instanceID) .build() val okhttpClIEnt = OkhttpClIEnt.Builder() .connectTimeout(1 , TimeUnit.MINUTES) .readTimeout(1, TimeUnit.MINUTES) .writeTimeout(1, TimeUnit.MINUTES) .build() Log.i(TAG, "Start Okhttp") okhttpClIEnt.newCall(request).enqueue(object : Callback { overrIDe fun onResponse(call: Call, response: Response) { if (!response.isSuccessful) { val message = response.body()?.string() ?: "Network Error" runOnUiThread { textOkhttpResult.text = message } return } runOnUiThread { textOkhttpResult.text = "OK" } val responseBody = response.body() Log.i(TAG, responseBody?.string()) } overrIDe fun onFailure(call: Call, e: IOException) { val message = e.message ?: "UnkNown Network error" runOnUiThread { textOkhttpResult.text = message } } }) } 总结 以上是内存溢出为你收集整理的java – Firebase云功能更改超时全部内容,希望文章能够帮你解决java – Firebase云功能更改超时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)