简体   繁体   English

我如何在我的 kivy 程序中使用 json api

[英]how do i use json api in my kivy program

hi I'm currently reading O'Reilly's Creating Apps in Kivy and there's an example that I can't get it to work correctly because by the time he wrote the book openWeatherMap didn't require api key (APPID) but now it does and I'm a novice programmer and don't know how to change the code so it would work.嗨,我目前正在阅读 O'Reilly 在 Kivy 中创建的应用程序,有一个例子我无法让它正常工作,因为当他写这本书时openWeatherMap不需要 api 密钥(APPID),但现在它确实需要我是一个新手程序员,不知道如何更改代码以使其正常工作。

this is the main.py source code:这是 main.py 源代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest
import json

class AddLocationForm(BoxLayout):
    search_input = ObjectProperty()

def search_location(self):
    search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"
    search_url = search_template.format(self.search_input.text)
    request = UrlRequest(search_url, self.found_location)

def found_location(self, request, 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']]
    self.search_results.item_strings = cities
    print("\n".join(cities))

class WeatherApp(App):
pass

if __name__ == '__main__':
WeatherApp().run()

and this is weather.kv source code:这是weather.kv 源代码:

AddLocationForm:

<AddLocationForm>:
    orientation: "vertical"
    search_input: search_box
    search_results: search_results_list
    BoxLayout:
    height: "40dp"
    size_hint_y: None
    TextInput:
        id: search_box
        size_hint_x: 50
    Button:
        text: "Search"
        size_hint_x: 25
        on_press: root.search_location()
    Button:
        text: "Current Location"
        size_hint_x: 25
ListView:
    id: search_results_list
    item_strings: []

the code's simple you put a city name in textbox and hit search and it confirms it by showing the name it recieved.代码很简单,你在文本框中输入一个城市名称并点击搜索,它通过显示它收到的名称来确认它。

OK, so I don't know if I'm late or not but having bought this book recently, I too found myself stuck exactly in this problem.好的,所以我不知道我是不是迟到了,但是最近买了这本书,我也发现自己完全陷入了这个问题。 Upon Googling this issue, I happened to stumble upon your question as well as O'Reilly link for this book.在谷歌搜索这个问题时,我偶然发现了你的问题以及本书的 O'Reilly 链接。 This is what the author had to say about this problem:对于这个问题,作者是这样说的:

"I've confirmed the issue; openweathermap has changed their query process and the urls in the book are now all broken. This is going to utterly ruin the reader experience for all new readers; we'll need to do an update and should maybe talk about a second edition." “我已经确认了这个问题;openweathermap 已经改变了他们的查询过程,书中的 url 现在都被破坏了。这将彻底破坏所有新读者的读者体验;我们需要做一个更新,也许应该谈论第二版。”

Luckily a good Samaritan found out the solution to this problem.幸运的是,一个好心的撒玛利亚人找到了解决这个问题的方法。 But in order to do so you must first create a free Open Weather account.但是为了做到这一点,您必须首先创建一个免费的 Open Weather 帐户。 After creating the account, you'll get an API key.创建帐户后,您将获得一个 API 密钥。 It'll be in your profile.它将在您的个人资料中。

So, now this code:所以,现在这个代码:

search_template = "http://api.openweathermap.org/data/2.5" + "find?q={}&type=like"

becomes:变成:

search_template = "http://api.openweathermap.org/data/2.5/find?q={}&type=like&APPID=" + "YOUR_API_KEY"

This worked for me.这对我有用。 I know I'm 3 months late and probably by now you'd have gotten your answer, but I thought this will be useful for those who run into similar problems and their Google result will bring them to this place.我知道我迟到了 3 个月,现在你可能已经得到了答案,但我认为这对遇到类似问题的人很有用,他们的谷歌结果会将他们带到这个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM