Java从另一个类获取链接(Json)

时间:2018-11-30 02:47:41

标签: java json webview

我有一个WebView,我想从另一个类获取此WebView的链接。 我的另一个类从JSON数据获取此链接。你能帮我吗?我是Java新手:/

Main.java;

btn.setOnAction(new EventHandler<ActionEvent>() { 
            @Override
            public void handle(ActionEvent event) {
                engine.load("https://www.google.com");
            }
        });

Reader.java(链接在这里)

public class Readerr {
    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }
    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static void main(String[] args) throws IOException, JSONException {
        JSONObject json = readJsonFromUrl("https://api.myjson.com/bins/cs3x6");
        String Link = json.get("link").toString();
    }
}

0 个答案:

没有答案