2015-12-17 28 views
1

我有'ArrayIndexOutOfBoundsException'错误onProgressUpdate函数 我认为任何事情都可以,但不能找到问题。 当这个任务要运行时logcat说ArrayIndexOutOfBoundsException Lenght = 0 index = 0 !!!我看到这个代码在这样的一些链接:onProgressUpdate上ArrayIndexOutOfBoundsException错误

How to implement file upload progress bar in android

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

和我tesed上传任务最前一页和它工作得很好....但我磨片添加进度条的崩溃...... 帮我解决,请

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

}

+0

您没有在第一个'publishProgress()'调用中传递任何值,但是您试图在'onProgressUpdate()'中检索该调用的第一个参数。看起来你并不需要第一个电话,所以你可以删除它,或者你可以通过'0'。 –

+0

是的...谢谢你...我看到那条线......但修复后,我跑了应用程序,看到进度条正在填补很快az上载....所以这里有什么问题? ? –

回答

1

好像你要发布你的进步w ^没有参数,所以你的onProgressUpdate()值数组是空的。

相关问题