gpt4 book ai didi

java - 使用网络库下拉 JSON

转载 作者:太空狗 更新时间:2023-10-29 15:53:35 26 4
gpt4 key购买 nike

我不经常使用 JSON,但我在没有其他库的情况下在 Android 中使用 JSON 时遇到了一些麻烦。我偶然发现了Ion by Koush我想用它来检索伦敦的天气数据。我将使用的 JSON api 来自 OpenWeather here .据我所知,JSON 是有效的,所以我现在停留在 Android 方面。我的最终目标是拥有一个报告 londons Main > temp 的对象/字符串。这是我的 Android 代码,但我不确定下一步该做什么。有什么想法吗?

private void getTemperature() {
Log.d(TAG, "2");
final JsonObject json = new JsonObject();
Ion.with(this, "http://api.openweathermap.org/data/2.5/weather?q=London,uk")
.asJsonObject().setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// do stuff with the result or error
Log.d("TAG", "Complete");
//json = result;
}
});
}

我的代码到达“完整”日志语句,但现在我试图将结果设置为我创建的 json 对象,所以我取消注释 //json = result,但我在 Eclipse 中出现错误。有什么帮助吗?我希望此方法能够简单地输出 Main -> Temp(截至目前)为 284.51。

最佳答案

典型的 ionic 使用是异步的,但它有同步支持。不要使用 setCallback(异步),而是使用 get()(同步)来同步返回它。不过,这会阻止您当前的线程。

final JsonObject json = Ion.with(this, "http://api.openweathermap.org/data/2.5/weather?q=London,uk")
.asJsonObject().
.get();

关于java - 使用网络库下拉 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21010178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com