【问题标题】:Java API call using JsonReader?使用 JsonReader 调用 Java API?
【发布时间】:2017-07-08 04:23:09
【问题描述】:

所以我不知道如何做到这一点:目标只是在 java 中调用 openweather api,并在控制台上返回结果。我找不到任何关于如何做到这一点的教程,只有关于如何从另一个文件中解析 JSON 数据的教程......

嗯,这是朝着正确的方向发展吗?不知道。根据建议修改以尝试使用 Gson

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;


public class ApiJSONRead {

    URL apiURL = new URL("http://api.openweathermap.org/data/2.5/find?q=London&APPID=(idhere)");

    public static void main(String[] args) throws IOException {



        JsonObject jobj = new Gson().fromJson(apiURL, JsonObject.class);
        var Jsonresponse = jobj.get("weather").getAsString();


        System.out.println(Jsonresponse);

    }

}

【问题讨论】:

  • 不知道:通过执行代码并阅读错误消息,您会得到一个想法。正如其名称(及其 javadoc)所示,FileInputStream 用于读取文件。不是来自 URL。如何从 URL 中获取 InputStream? URL 的 javadoc 可能会告诉你。

标签: java json openweathermap jsonreader


【解决方案1】:

有点,尽管正如其中一位评论者指出的那样,运行你的代码。它行不通。一旦你得到了流,你所拥有的应该可以工作。但是apiURL 不是文件,所以FileInputStream 不会理解如何阅读它。查看Apache HttpComponents。具体来说,here's 是一个示例,它展示了如何发出 GET 请求,这正是您想要做的。

【讨论】:

    【解决方案2】:

    解析JSON数据可以使用google API gson

    例如

        JsonObject jobj = new Gson().fromJson(response, JsonObject.class);
        jsonresponse = jobj.get("message").getAsString();
    

    【讨论】:

    • 谢谢 Vikrant,您有更完整的用法示例吗?我只能找到使用文件的示例,但没有任何关于如何从 api url 读取数据的示例...
    • 你可以使用apache http客户端点击url,HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("ip.jsontest.com/"); org.apache.http.HttpResponse httpResponse = client.execute(request); String response = EntityUtils.toString(httpResponse.getEntity()); System.out.println(response) ;
    【解决方案3】:

    检查以下代码并导入必要的 jars

    import java.io.IOException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    import com.google.gson.Gson;
    import com.google.gson.JsonObject;
    
    
    public class ApiJSONRead {
    
    
    
        public static void main(String[] args) throws IOException {
    
    
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://ip.jsontest.com/");
            org.apache.http.HttpResponse httpResponse = client.execute(request);
            String response = EntityUtils.toString(httpResponse.getEntity());
            System.out.println(response);
            JsonObject jobj = new Gson().fromJson(response, JsonObject.class);
            String Jsonresponse = jobj.get("ip").getAsString();
            System.out.println(Jsonresponse);
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      我是 Sprinkler 开源项目的提交者。见http://wiki.bitplan.com/index.php/Sprinkler 作为项目的一部分,我正在实施必要的开放天气 API 调用。 你可能想看看

      https://github.com/BITPlan/com.bitplan.sprinkler/blob/master/src/test/java/com/bitplan/sprinkler/TestWeatherReport.java

      https://github.com/BITPlan/com.bitplan.sprinkler/blob/master/src/main/java/org/openweathermap/weather/WeatherReport.java

      还有一个 JsonUtil 帮助类,它隐藏了如何从 url 获取 json 字符串的细节。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多