gpt4 book ai didi

android - 我需要知道已经上传了多少字节来更新进度条android

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:57 26 4
gpt4 key购买 nike

我正在开发一个用于将视频上传到 Apache/PHP 服务器的应用程序。这一刻我已经可以上传视频了。我需要在上传文件时显示进度条。我有下一个代码使用 AsyncTask 和 HTTP 4.1.1 库来模拟 FORM。

class uploadVideo extends AsyncTask<Void,Void,String>{

@Override
protected String doInBackground(Void... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.youtouch.cl/videoloader/index.php");
try {
// Add your data
File input=new File(fileName);

MultipartEntity multi=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

multi.addPart("video", new FileBody(input));

httppost.setEntity(multi);

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

HttpEntity entity = response.getEntity();

BufferedReader reader = new BufferedReader(
new InputStreamReader(
entity.getContent(), "UTF-8"));
String sResponse = reader.readLine();
return sResponse;

} catch (ClientProtocolException e) {
Log.v("Uri Galeria", e.toString());
e.printStackTrace();

} catch (IOException e) {
Log.v("Uri Galeria", e.toString());
e.printStackTrace();
}
return "error";
}

@Override
protected void onProgressUpdate(Void... unsued) {
//Here I do should update the progress bar
}

@Override
protected void onPostExecute(String sResponse) {
try {
if (pd.isShowing())
pd.dismiss();

if (sResponse != null) {
JSONObject JResponse = new JSONObject(sResponse);
int success = JResponse.getInt("SUCCESS");
String message = JResponse.getString("MESSAGE");
if (success == 0) {
Toast.makeText(getApplicationContext(), message,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Video uploaded successfully",
Toast.LENGTH_SHORT).show();

}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}

我需要知道从哪里可以获得已上传的字节数。 File.length 是总大小。

最佳答案

您是否尝试过扩展 FileBody?据推测,POST 将调用 getInputStream()writeTo() 以实际将文件数据发送到服务器。您可以扩展其中任何一个(包括 getInputStream() 返回的 InputStream)并跟踪已发送的数据量。

关于android - 我需要知道已经上传了多少字节来更新进度条android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6851024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com