如何正确设置进度值?

时间:2014-03-27 20:02:46

标签: android android-progressbar fileinputstream android-file dataoutputstream

我在google和stackoverflow上搜索了很多次,但我找不到真正需要的东西。 我写了一个代码,用asynctask方法将文件上传到服务器。 我做的是: 在asyntask方法中,我使用 post 方法将我的文件上传到我的 webservice 。 在 doInBackground 方法中:

int serverResponseCode = 0;
try {

    // open a URL connection to the Servlet
    FileInputStream fileInputStream = new FileInputStream(sourceFile);

    // ------------------ CLIENT REQUEST
    URL url = new URL(upLoadServerUri);

    // Open a HTTP connection to the URL
    conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true); // Allow Inputs
    conn.setDoOutput(true); // Allow Outputs
    conn.setUseCaches(false); // Don't use a Cached Copy

    // Use a post method.
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
    conn.setRequestProperty("Content-Type",
        "multipart/form-data;boundary=" + boundary);
    conn.setRequestProperty("file_name", fileName);
    conn.setRequestProperty("file_name_audio", fileName); 
    // conn.setFixedLengthStreamingMode(1024);
    // conn.setChunkedStreamingMode(1);

    dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);

    dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\""
        + fileName + "\"" + lineEnd);

    dos.writeBytes(lineEnd);


        // create a buffer of maximum size
    bytesAvailable = fileInputStream.available();

    int streamSize = (int) sourceFile.length();
    bufferSize = streamSize / 4;

    buffer = new byte[streamSize];
    int sentBytes=0;
    // read file and write it into form...
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    while (bytesRead > 0) {  
    Thread.sleep(2);  
    sentBytes += bufferSize;
    int progress = (int)((sentBytes / (float) bytesAvailable) * 100);
    publishProgress( progress+""); 
    dos.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    // bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    }

    // send multipart form data necesssary after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    serverResponseCode = conn.getResponseCode();
    @SuppressWarnings("unused")
    String serverResponseMessage = conn.getResponseMessage();

    fileInputStream.close();  
    dos.flush();
    dos.close();

} catch (MalformedURLException ex) {
    ex.printStackTrace();
     Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
    e.printStackTrace();
}

String serverResponseMessage = conn.getResponseMessage();时间更长

我的uploadprogress设置了4次,然后我等待更多时间将我的文件上传到服务器。为什么我需要代码退出while。如何设置进度条值更具敏感性以及何时{{1值设置100,我想文件上传真的完成了。 我设置了onpreexecute和onpostexecute方法。但我认为他们没有必要在这里写。 我使用progressbar进度,所以我必须显示百分比。

2 个答案:

答案 0 :(得分:0)

AsynTask中有几种方法可以覆盖,其中一种方法如下:

void onPreExecute()
{
   //here you can show your progress bar
}

然后在完成任务后会调用另一个方法:

void onPostExecute(String result)
{
    //here you can dismiss your progressbar
}

现在,据我所知,您可以使用不同类型的酒吧来显示您的进度;从显示圆圈的简化版本到具有不同完成级别的水平圆圈。

如果您显示圆形的,则不必显示百分比,但如果您使用水平的,则必须进行一些计算并相应地进行设置!

请记住,您可以在doInBackground中更新进度条,具体取决于剩余的工作量!

我希望这有帮助!

答案 1 :(得分:-1)

这是我在我的一个项目中实施的步骤,这种情况可能对您的情况有所帮助。

  • 创建一个可上传文件的可运行线程。该类包含一个get方法,我们使用此方法计算进度条值。
  • get方法包含逻辑(比如获取文件的大小并需要您上传的数据,从这些值中您将获得可用于进度条的百分位数)。
  • 创建一个设置进度条值的计时器。
  • 在流程开始前启动计时器
  • 定时器线程,用于设置进度条值并上传线程上传并提供进度条值。

我做到了这一点并且有效。我希望这可能会有所帮助。

由于