我有一个问题,自过去两周以来无法解决。我在这里需要一些帮助。我实际上想从 HTTP 网站获取和使用一些有用的数据。该网站实际上包含事故、事件和有关它们的所有信息。我想从网站上获取这些信息。我将在我的 Android 应用程序中使用它。我已经问过这个问题,但仍然无法解决。有人告诉我,你必须从 JSON 中获取这些数据。我以前没有这样做过。如果这是唯一的解决方案,那么我该怎么做。如果还有其他简单的方法,请给我。我实际上已经通过使用获得了所有网站内容
private String DownloadText(String URL) {
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception in downloadText";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
return str;
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
但它提供了所有内容,即所有信息+html+xml+++。但我只想要必需的信息。
另一件事是,在获取该数据之前是否必须获得网站管理员许可?