从网站上阅读

Hyp*_*erX 0 java android

我正在制作一个小程序,它将从网站上读取数据.html文件中的字符串已经被管理,每个信息被分成; .现在我应该读完整行这里是这一行的例子:

14:47;24.02.12;18.7°C;18.7°C;285;0.5m/s; 6:48;17:37; Warm ;36;1.8;0.0;
Run Code Online (Sandbox Code Playgroud)

首先,我应该如何使用HTTP Get读取它们还是还有其他什么?然后我想保存每个信息,它们是分开的; 变成一个变量.我应该如何从这一行切割每个信息.

Tch*_*upi 5

你肯定需要做一些功课,但这种方法可以帮助你:

public static String getContentFromUrl(String url) throws ClientProtocolException, IOException {

    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response;

    response = httpClient.execute(httpGet);
    HttpEntity entity = response.getEntity();

    if (entity != null) {

        InputStream inStream = entity.getContent();

        String result = HttpService.convertStreamToString(inStream);
        inStream.close();

        return result;
    }

    return null;
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)

这允许您从URL获取数据.然后查找String.split将您的字符串切换为可用实体.

希望这可以帮助!