JSON显示在网站正文中而不是源代码中。如何用android阅读?

时间:2017-11-07 22:13:09

标签: javascript java android html json

我正在尝试使用此代码(在javascript中)从一个返回JSON数据的网站读取我的Android应用程序JSON数据:

 document.body.innerHTML = "<pre style='word-wrap: break-word; white-space: pre-wrap;'>" + JSON.stringify(address, null, 4) + "</pre>";

示例网站可在此处找到:Link

问题是从android获取此网站会返回完整的html源代码,而不是仅向我提供带有JSON数据的正文。

android中获取此代码的代码如下所示:

URL url = new URL(apiURL);
connection =  url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
    buffer.append(line+"\n");
    Log.d("Response: ", "> " + line);  
}

所以我不知道如何管理这个...我可以修改网站html或Android应用程序,但我已经尝试了一切,但我找不到解决方案。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

尝试删除不需要的HTML

URL url = new URL(apiURL);
connection =  url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
    buffer.append(line+"\n");
    Log.d("Response: ", "> " + line);  
}
// remove <pre> tag
line = line.replace("<pre style='word-wrap: break-word; white-space: pre-wrap;'>","").replace("</pre>","");