onProgressUpdate上的ArrayIndexOutOfBoundsException错误

时间:2015-12-17 20:23:42

标签: android android-asynctask progress-bar

我在函数onProgressUpdate上有'ArrayIndexOutOfBoundsException'错误 我认为一切都好,但无法找到问题。 当这个任务要运行时,logcat说ArrayIndexOutOfBoundsException Lenght = 0 index = 0 !!!我在这样的链接中看到了这段代码:

How to implement file upload progress bar in android

Upload large file with progress bar and without OutOfMemory Error in Android

我把上传任务fisrt和它工作得很好....但是当我添加进度条时它崩溃了...... 请帮我解决

public class InsertFile extends AsyncTask<String, Integer, String> {

final String name;
final Context parent;
String sourceFileUri;
String upLoadServerUri;
int allByte,perBytes=0;
SeekArc skarc;
private ProgressDialog dialog;

public InsertFile(Context c,String uri,String name,String folder2Up, SeekArc s){
    parent = c;
    sourceFileUri=uri;
    this.name=name;
    upLoadServerUri="*******************************";
    //skarc=s;
    dialog = new ProgressDialog(parent);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setMessage("Uploading photo, please wait.");
    dialog.setMax(100);
    dialog.setCancelable(true);
}


@Override
protected String doInBackground(String... params) {

    try {

        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File sourceFile = new File(sourceFileUri);
        publishProgress();
        if (sourceFile.isFile()) {

            try {
                // open a URL connection to the Servlet
                FileInputStream fileInputStream = new           FileInputStream(sourceFile);
                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
                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("bill", sourceFileUri);

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

                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"bill\";filename=\""
                        + name + "\"" + lineEnd);

                dos.writeBytes(lineEnd);

                // create a buffer of maximum size
                bytesAvailable = fileInputStream.available();
                allByte=bytesAvailable;
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];

                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                perBytes=0;
                perBytes=((allByte-bytesAvailable)*100)/allByte;

                while (bytesRead > 0) {
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    perBytes=((allByte-bytesAvailable)*100)/allByte;
                    publishProgress(Integer.valueOf(perBytes));
                }

                // 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();
                //String serverResponseMessage = conn
                //       .getResponseMessage();

                //if (serverResponseCode == 200) {

                // messageText.setText(msg);
                //Toast.makeText(ctx, "File Upload Complete.",
                //      Toast.LENGTH_SHORT).show();

                // recursiveDelete(mDirectory1);

                //}

                int serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();

                Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

                if (serverResponseCode == 200) {
                    Handler handler = new Handler(parent.getMainLooper());
                    handler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(parent, "File is uploaded", Toast.LENGTH_LONG).show();
                        }
                    });
                }
                // close the streams //
                fileInputStream.close();
                dos.flush();
                dos.close();


            }catch(Exception e){

                e.printStackTrace();

            }
        }
        else{
            Handler handler =  new Handler(parent.getMainLooper());
            handler.post(new Runnable() {
                public void run() {
                    Toast.makeText(parent, "No file", Toast.LENGTH_LONG).show();
                }
            });
        }

    }

    catch (final Exception ex) {

        ex.printStackTrace();
        Handler handler =  new Handler(parent.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                Toast.makeText(parent, ex.toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    return "Executed";
}

@Override
protected void onProgressUpdate(Integer... values) {
    dialog.setProgress(values[0]);
}

@Override
protected void onPostExecute(String result) {
    dialog.dismiss();
}

@Override
protected void onPreExecute() {
    dialog.show();
}

}

1 个答案:

答案 0 :(得分:1)

好像你在没有参数的情况下发布你的进度,所以你的onProgressUpdate()values数组是空的。