【问题标题】:HttpGet JSONArray android ProgressbarHttpGet JSONArray android 进度条
【发布时间】:2012-09-02 05:15:02
【问题描述】:


我正在尝试为从 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 方法。

【问题讨论】:

    标签: java php android json http


    【解决方案1】:

    在此处更新您的进度:

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

    您可以使用AsyncTask.publishProgressAsyncTask.onProgressUpdate 方法。

    【讨论】:

      【解决方案2】:

      获取size of the http responseset the progress bar max 的值,然后在while 循环中获取update the progress bar progress

      【讨论】:

      • 服务器可能没有给出Content-Length,这种情况下你注定要使用不确定的样式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2011-11-10
      • 2011-02-02
      • 1970-01-01
      • 2014-08-05
      • 2016-04-21
      相关资源
      最近更新 更多