有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓在java方面缺乏经验。在REST API客户端中摸索

我正在拼凑一个小的安卓应用程序,我想为我们的团队使用。它所做的只是调用REST API端点并执行一系列查询。状态、警报和一些监控信息。很简单。但我花了三天时间关注谷歌上的每个网站,这些网站解释了如何用Java进行REST API调用,获取JSON,然后将其存储为类似于散列的内容,然后我可以通过名称引用JSON元素。我用Python编写了一个桌面版本,我在想Python只是把它解析成一个DICT。我甚至不确定它是否有效,因为我被下一步该做什么所困扰:

protected void getJSON() {
    String url = "http://192.168.8.29/api/v1/version";

    try {

        URL hmAPIEndPoint = new URL(url);
        HttpURLConnection myConnection = (HttpURLConnection) hmAPIEndPoint.openConnection();

        if (myConnection.getResponseCode() == 200) {
            // Success
            // Further processing here
            Log.d("getJSON", "Got a 200 back from the openConnection()");

            InputStream responseBody = myConnection.getInputStream();
            InputStreamReader responseBodyReader = new InputStreamReader(responseBody, "UTF-8");

            JsonReader jsonReader = new JsonReader(responseBodyReader);

            // I need jsonReader to be something I can reference like jsonReader['version_id']

            jsonReader.close();
            myConnection.disconnect();
        } else {
            Log.e("getJSON", "Didn't register a 200 response. Actual: " + myConnection.getResponseCode());
        }

    } catch (MalformedURLException mfe) {
        Log.e("getJSON", mfe.getStackTrace().toString());
    } catch (Exception e) {
        Log.e("getJSON", e.getStackTrace().toString());
    }
}

从这里开始,大约有500个网站告诉我只需逐行解析json,但这似乎。。。愚蠢的有没有一种方法可以将jsonReader东西放入上面描述的对象中

提前感谢你的帮助


共 (0) 个答案