1

i have 'ArrayIndexOutOfBoundsException' error on function onProgressUpdate i think anything is ok but cant find the problem. when this task is going to run logcat says ArrayIndexOutOfBoundsException Lenght=0 index=0 !!! i see this code in some link like these:

How to implement file upload progress bar in android

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

and i tesed the upload task fisrt and its worked fine....but whe i add progress bar its crashed... help me to solve please

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();
}

}

Community
  • 1
  • 1
  • You're not passing any values in the first `publishProgress()` call, but you're trying to retrieve the first argument of that call in `onProgressUpdate()`. It doesn't look like you really need the first call, so you could just remove it, or you could pass `0`. – Mike M. Dec 17 '15 at 20:32
  • yes...thank u...i dident see that line.....but after fixing this i ran the app and see the progressbar is filling very soon az uploading....so what is the problem here ??? – Nakhoda Sokoot Dec 17 '15 at 20:58

1 Answers1

1

Seems like you're publishing your progress with no arguments, so your onProgressUpdate() values array is empty.

Carter Hudson
  • 1,176
  • 1
  • 11
  • 23