- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我为此苦苦挣扎了一段时间,但找不到解决方案。
所以我通过 Dusty Phillips 的“在 Kivy 中创建应用程序”学习 Python 和 Kivy。这是一个简单的天气应用程序,当我尝试从 openweathermap.com 获取数据时,UrlRequest 函数无法正常工作。我对 kivy 和 python 很陌生,但正如我所见,该函数必须使用两个参数调用“found_location”方法:请求和结果(从 url 获得的列表)。如果我从浏览器访问 url,我会得到正确的结果,但返回 python,“结果”为 NONE。
这是带有一些用于调试的打印的代码:
from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
search_results = ObjectProperty()
def search_location(self):
search_template = "api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=" + "{}"
search_url = search_template.format(self.search_input.text)
print search_url
request = UrlRequest(search_url, self.found_location)
print request
print "Result: ", request.result
def found_location(self, request, data):
print request
print data
data = json.loads(data.decode()) if not isinstance(data, dict) else data
cities = ["{} ({})".format(d['name'], d['sys']['country'])
for d in data['list']]
print cities
self.search_results.item_strings = cities
print "DONE"
class WeatherApp(App):
pass
if __name__ == '__main__':
WeatherApp().run()
这里是控制台:
[INFO ] [OSC ] using <multiprocessing> for socket
[INFO ] [Base ] Start application main loop
[INFO ] [GL ] NPOT texture support is available
api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=London
<UrlRequest(Thread-1, started daemon 139654193755904)>
Result: None
如您所见,它传递了正确的 URL,在浏览器中我得到了正确的结果,但从未调用“found_location”方法,在 python 中 request.results = None
我做错了什么?
希望你们能理解我的问题。感谢您的帮助,对英语感到抱歉。
最佳答案
这里的问题是你在下载成功之前打印结果。
还要记得在链接字符串前加上“http://”。
请记住,url 是异步加载的。正如 UrlRequest 上的文档中所说
You can use the UrlRequest to make asynchronous requests on the web and get the result when the request is completed. The spirit is the same as the XHR object in Javascript.
这就是为什么要在 UrlRequest 中使用 on_success 参数
我给你举个例子
from kivy.app import App
#kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
class MyWidget(BoxLayout):
def __init__(self,**kwargs):
super(MyWidget,self).__init__(**kwargs)
search_url = "http://api.openweathermap.org/data/2.5/forecast/daily?APPID=ef4f6b76310abad083b96a45a6f547be&q=new%20york"
print search_url
self.request = UrlRequest(search_url, self.res)
print self.request
print "Result: before success", self.request.result,"\n"
def res(self,*args):
print "Result: after success", self.request.result
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
关于Python/Kivy - UrlRequest 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471829/
我如何为应用程序中的所有 URLRequest 在 URLRequest 和 URLSession 中添加自定义拦截器。所以我可以在一个地方为所有 http 请求添加我自己的自定义 HTTPHeade
对不起,我的英语不好 我尝试了多种类型的解决方案,但无法在Xcode 11.2.1和swift 5中使用 我尝试这个 var urlRequest = URLRequest(url: URL(stri
我正在尝试对 URLRequest 进行扩展,允许它使用关联的对象进行初始化,然后能够稍后检索该对象以了解该请求是否“属于”该对象。我的第一次尝试是: class User { init(_
我正在使用下面的代码来创建 URLRequest,但特殊字符(如 &)未正确编码。我做错了什么? var request = URLRequest(url: URL(string: "http:/
什么是最好的选择: URL 请求 NSURLConnection ASIHTTP请求 AFNetwork JSON 解析 NSJSON序列化 SBJSON 我选择了 ASIHTTPRequest+NS
在下面的代码中 let bodyData = "?sub=\(id)&name=User&email=test@test.com" let url = NSURL(string: "h
我正在尝试创建一个 createRequest 函数,我可以将其重新用于我的所有网络调用,有些需要发布 JSON 而其他则不需要,所以我正在考虑创建一个采用可选通用对象的函数;理论上是这样的: str
尝试编写可维护的、干净的代码的结果: func fetchNumbersFromServer(completion: @escaping (NumbersResult) -> Void) {
我有一个使用以下代码设置的简单 PHP 服务文件: 在我的应用程序中,我使用以下代码调用它: func postData(dictData:Dictionary, method:String) {
我想为不同的请求设置不同的超时时间。我的请求例程如下所示: var request = URLRequest(url: url, cachePolic
我有 webView,我在其中加载了一些 url。我需要为该 URLRequest 设置自定义 header 。对于第一个请求,它按预期工作,在服务器端收到 header 并相应地显示内容。但是,如果
我是Objective-C的入门者,我有一种方法 - (void)getAltitudeFromElevationFromAlt:(float)latitude Long:(float)longitu
这个问题已经有答案了: Returning data from async call in Swift function (13 个回答) 已关闭 5 年前。 我尝试在 func 中创建请求,并在调用
当我在我的应用程序中按下登录时,它在 API 中登录两次而不是一次..,这有问题但我找不到什么,因为它只执行此代码一次。 NSUserDefaults *defaults =[NSUserDef
我已经开始学习 ios 开发一段时间了,我已经达到了尝试从服务器发送和接收数据的部分。我遇到了一个问题,例如,如果我想让用户登录或使用异步连接让他注册。使用 swiftyjson 的数据管理器类的代码
我必须在浏览许多网站后一次调用 20 个 urlReqests 我发现 4 个 urlRequsts 被处理之后下一个正在进行的请求将遇到超时错误的错误。当我在一个循环中调用 5 个 web 服务时,
我按如下方式从我的服务器加载我的 json 信息,但是当我过早地点击离开到另一个页面时,请求一直在后台尝试,并且有一条警告说再也找不到 viewController 了。如何取消所有 onViewDi
我想从 url 获取数据,但在过程完成之前,我想显示加载图标 我使用此代码但不适合我 DispatchQueue.global(qos: .background).async { // sho
我正在用 flask sqlalchemy 和 marshmallow 编写一个 flask 后端,它返回 json 和 kivy 作为前端。我正在尝试使用 kivy urlrequest 登录我的
我为此苦苦挣扎了一段时间,但找不到解决方案。 所以我通过 Dusty Phillips 的“在 Kivy 中创建应用程序”学习 Python 和 Kivy。这是一个简单的天气应用程序,当我尝试从 op
我是一名优秀的程序员,十分优秀!