大佬教程收集整理的这篇文章主要介绍了android – 如何从字节数组上传数据到PHP服务器上传进度?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
http://loopj.com/android-async-http/
我想要的是在进度条上显示已上载字节数组的字节数.现在,问题是,如何在上传进行过程中了解已上传的字节数.需要有关想法/示例/代码问题的帮助.先谢谢你们.
class ImageUploadTask extends AsyncTask<Void,Void,String> { @Override protected void onPreExecute() { pb.setVisibility(View.VISIBLE); } @Override protected String doInBACkground(Void... unused) { String twoHyphens = "--"; String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****"; String lineEnd = "\r\n"; try { FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFilE)); URL url = new URL(urlServer); connection = (httpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setrequestMethod("POST"); connection.setrequestProperty("Connection","Keep-Alive"); connection.setrequestProperty("Content-Type","multipart/form-data;boundary="+boundary); outputStream = new DataOutputStream( connection.getOutputStream() ); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); Log.v("Size",bytesAvailable+""); pb.setProgress(0); pb.setMax(bytesAvailablE); //Log.v("Max",pb.getMax()+""); bufferSize = Math.min(bytesAvailable,maxBufferSizE); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer,bufferSizE); while (bytesRead > 0) { outputStream.write(buffer,bufferSizE); bytesAvailable = fileInputStream.available(); Log.v("Available",bytesAvailable+""); publishProgress(); bufferSize = Math.min(bytesAvailable,maxBufferSizE); bytesRead = fileInputStream.read(buffer,bufferSizE); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and messagE) serverResponseCode = connection.getResponseCode(); serverResponsemessage = connection.getResponsemessage(); System.out.println(serverResponsemessagE); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception eX) { //Exception handling } //publishProgress(); return null; } @Override protected void onProgressupdate(Void... unsued) { super.onProgressupdate(unsued); pb.setProgress(pb.getMax()-bytesAvailablE); } @Override protected void onPostExecute(String sResponsE) { //if(pb.getProgress()>= pb.getMax()) pb.setVisibility(View.INVISIBLE); } }
以上是大佬教程为你收集整理的android – 如何从字节数组上传数据到PHP服务器上传进度?全部内容,希望文章能够帮你解决android – 如何从字节数组上传数据到PHP服务器上传进度?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。