当前位置: 首页 > 知识库问答 >
问题:

为什么调用API的函数返回空值或空值?

龙宣
2023-03-14

(免责声明:人们通过facebook、firebase等请求询问使用异步操作时数据为空/不正确时,会产生很多问题。我提出这个问题的目的是为每个从android异步操作开始的人提供一个简单的答案。)

我试图从我的一个操作中获取数据,当我使用断点或日志调试它时,值就在那里,但当我运行它时,它们总是空的,我该如何解决这个问题呢?

火力基地

firebaseFirestore.collection("some collection").get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                     //I want to return these values I receive here? 
            })

脸书

GraphRequest request = GraphRequest.newGraphPathRequest(
            accessToken,
            "some path",
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse response) {
                     //I want to return these values I receive here? 
                }
            });
    request.executeAsync();

等。

共有1个答案

胡向阳
2023-03-14

什么是同步/异步操作?

同步会一直等到任务完成。在这种情况下,您的代码是“自顶向下”执行的。

Asynchronous在后台完成任务,完成时可以通知您。

如果希望通过方法/函数从异步操作返回值,可以在方法/函数中定义自己的回调,以便在这些操作返回值时使用这些值。

以下是Java的方法

首先定义接口:

interface Callback {
 void myResponseCallback(YourReturnType result);//whatever your return type is: string, integer, etc.
}

接下来,将您的方法签名更改为如下所示:

public void foo(final Callback callback) { // make your method, which was previously returning something, return void, and add in the new callback interface.

接下来,在您以前想要使用这些值的地方,添加这一行:

   callback.myResponseCallback(yourResponseObject);

例如:

 @Override
 public void onSuccess(QuerySnapshot documentSnapshots) {
 // create your object you want to return here
 String bar = document.get("something").toString();
 callback.myResponseCallback(bar);
 })

现在,您以前调用方法foo的位置:

foo(new Callback() {
        @Override
        public void myResponseCallback(YourReturnType result) {
            //here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
        }
    });
}

你是怎么为Kotlin做这些的?(作为您只关心单个结果的基本示例)

首先将方法签名更改为如下所示:

fun foo(callback:(YourReturnType) -> Unit) {
.....

然后,在异步操作的结果中:

firestore.collection("something").document("document").get().addOnSuccessListener { 
                    val bar = it.get("something").toString()
                    callback.invoke(bar)
                }

然后,在以前调用方法foo的地方,现在执行以下操作:

foo { result->
here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
}

如果foo方法以前接受了参数:

fun foo(value:SomeType, callback:(YourType) -> Unit)

您只需将其更改为:

foo(yourValueHere) { result ->
here, this result parameter that comes through is your api call result to use, so use this result right here to do any operation you previously wanted to do. 
    }

这些解决方案展示了如何创建一个方法/函数,以从通过使用回调执行的异步操作返回值。

但是,重要的是要理解,如果您不想为这些创建方法/函数:

@Override
 public void onSuccess(SomeApiObjectType someApiResult) {
//here, this `onSuccess` callback provided by the api already has the data you're looking for (in this example, that data would be `someApiResult`).
//you can simply add all your relevant code which would be using this result inside this block here, this will include any manipulation of data, populating adapters, etc. 
//this is the only place where you will have access to the data returned by the api call, assuming your api follows this pattern
     })
 类似资料:
  • (免责声明:当人们通过Facebook、Firebase等请求使用异步操作时,会问到数据为空/不正确的问题。我提出这个问题的目的是为每个开始在android中进行异步操作的人提供一个简单的答案) 我试图从我的一个操作中获取数据,当我使用断点或日志调试它时,值就在那里,但当我运行它时,它们总是空的,我如何解决这个问题? 火基 脸谱网 静态编程语言协程 等等。

  • 为什么代码获取不到API返回的数据? API是免费版本, 每分钟限制45次请求,使用POST请求方式, URL中的fields=58898是返回指定字段. 我在API的网站查询IP没有问题, 但是代码中一直返回空值, requests也没有报错, 所以现在有点不知道从哪里下手了, 请大佬们帮我分析一下, 或者给点思路! 跪谢!

  • 当我在输入元素中输入第一个字符时,我得到“空字符串”。 在我看来,我在输入元素中输入一个字符,触发“onChange”事件,运行函数getTitle,设置“title”变量,该变量连接到useState钩子,然后控制结果。按照这个推理,我希望输入第一个字符。相反,我得到的是“空字符串”。从第二个字符开始,控制台打印字符。 对于“onInput”,函数也会发生同样的情况。 如何解决这个问题,为什么会

  • 问题内容: 我是一名编程初学者,对函数的返回值有疑问。 我正在学习Java。 我已经附上了我的书中具有经典选择排序功能的代码。 现在显然来自本书的代码可以正常工作。但是,主要功能中的以下三行是我的问题的基础: int [] a = new int [] {1,9,2,8,3,7,4,6​​,5}; 排序(a); if(ascending(a))System.out.println(“ Works”

  • 问题内容: 我正在使用Postgresql 8.3,并具有以下简单功能,该功能会将a返回 给客户端 现在,我可以使用以下SQL命令来调用此函数并操纵返回的游标,但是游标名称是由PostgreSQL自动生成的 此外,如38.7.3.5中所述,显式地将游标名称声明为函数的输入参数 。返回游标。我可以声明自己的游标名称并使用此游标名称来操纵返回的游标,而不是为我自动生成的Postgresql吗?如果不是