2


我正在尝试为从 Internet 下载的 json 数组获取水平进度条对话框。这是我当前的代码

protected JSONArray doInBackground(Date... arg0) {
  try {
    BufferedReader in = null;
    String result = null;

    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setSoTimeout(client.getParams(), 30000);
    HttpConnectionParams.setConnectionTimeout(client.getParams(),
            30000);

    String getter = arg0[0].getDate();

    URI website = new URI("http://10.0.2.2/bmorg/getEvent?day=" + getter);

    HttpGet request = new HttpGet();
    request.setURI(website);

    HttpResponse response = client.execute(request);

    in = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));

    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.seperator");
    while ((l = in.readLine()) != null) {
      sb.append(l + nl);
    }

    in.close();
    result = sb.toString();

    return new JSONArray(result);
  } catch (Exception e) {
    _worked = false;
    e.printStackTrace();
  }
  return null;
}

这工作正常,但我想要的是下载状态。我不知道如何从我的 Http 请求和下载的字节中获取大小以将其放入进度条的 onChange 方法。

4

2 回答 2

0

在此处更新您的进度:

while ((l = in.readLine()) != null) {
      sb.append(l + nl);
    }

你可以使用它AsyncTask.publishProgressAsyncTask.onProgressUpdate方法。

于 2012-09-09T13:21:44.537 回答
0

获取http响应的大小设置进度条最大值,然后在循环中更新进度条进度。while

于 2012-09-10T10:46:57.740 回答