First of all, you are using an asynchronous request to access the network, so it is impossible to get the value synchronously when you call it. Either use OKHTTP's synchronous access to the network and return the value after the network access is completed, so that you can get the correct value, or you can control the asynchronous acquisition of the value yourself, such as getting the value after the network request is completed, that is, entering Do the operation after response.
We do not write the inner class directly here, but let the current class inherit the interface and assign the value to the variable of the current class in the overridden method. This is my idea
The getAsyn method is an asynchronous method from the name. Your getUrl will return before it receives the response, so the value in the response cannot be obtained. There are 2 solutions: getAsyn方法从名字来看是一个异步方法,你的getUrl在它收到响应之前就会返回,所以无法得到响应中的值。有2种解决办法:
See if OkHttpClientManager has a synchronization method
🎜If you must use an asynchronous method, you can send an asynchronous request in getUrl and wait in a sleep or loop until parseJson has a value before returning. This actually turns asynchronous into synchronous in disguise. 🎜🎜
🎜
First of all, you are using an asynchronous request to access the network, so it is impossible to get the value synchronously when you call it. Either use OKHTTP's synchronous access to the network and return the value after the network access is completed, so that you can get the correct value, or you can control the asynchronous acquisition of the value yourself, such as getting the value after the network request is completed, that is, entering Do the operation after response.
We do not write the inner class directly here, but let the current class inherit the interface and assign the value to the variable of the current class in the overridden method. This is my idea
The
getAsyn
method is an asynchronous method from the name. YourgetUrl
will return before it receives the response, so the value in the response cannot be obtained. There are 2 solutions:getAsyn
方法从名字来看是一个异步方法,你的getUrl
在它收到响应之前就会返回,所以无法得到响应中的值。有2种解决办法:看看
OkHttpClientManager
有没有同步方法如果非要使用异步方法,可以在
getUrl
中发送异步请求之后用休眠或循环的方式等待,直到parseJson
OkHttpClientManager
has a synchronization methodgetUrl
and wait in a sleep or loop untilparseJson
has a value before returning. This actually turns asynchronous into synchronous in disguise. 🎜🎜 🎜Personally, I think what the 1st floor said is quite right