2014-02-23 87 views
0

我使用AsyncTask將文件發送到server.Asynctask是一個不在MainActivity中的不同類文件。我面臨的問題是當我在MainActivity類中寫入AsyncTask時。一切都很好。但是當我寫不同的Java類,並從MainActivity訪問該類發送參數我得到這些錯誤android-將參數傳遞給Asynctask

02-23 01:06:46.995: I/System.out(1191): path is[Ljava.lang.String;@b1dccd40 
02-23 01:06:47.125: E/Debug(1191): error: /[Ljava.lang.String;@b1dccd40: open failed: ENOENT (No such file or directory) 
02-23 01:06:47.125: E/Debug(1191): java.io.FileNotFoundException: /[Ljava.lang.String;@b1dccd40: open failed: ENOENT (No such file or directory) 

這裏是我的代碼

public class ASync extends AsyncTask<String, Void, String> 
{ 

    protected String doInBackground(String... path) { 
     System.out.println("path is" + path); 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 4*1024*1024; 
     String responseFromServer = ""; 
     String urlString = "http://path"; 
     try 
     { 
    /*----- i think this line is causing an error */ 
     FileInputStream fileInputStream = new FileInputStream(new File(path.toString())); 
     /**----/  
      // open a URL connection to the Servlet 
      URL url = new URL(urlString); 
      // Open a HTTP connection to the URL 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setConnectTimeout(7000); 
      // Allow Inputs 
      conn.setDoInput(true); 
      // Allow Outputs 
      conn.setDoOutput(true); 
      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      // Use a post method. 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
      dos = new DataOutputStream(conn.getOutputStream()); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + path + "\"" + lineEnd); 
      dos.writeBytes(lineEnd); 
      // create a buffer of maximum size 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 
      // read file and write it into form... 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      while (bytesRead > 0) 
      { 
      dos.write(buffer, 0, bufferSize); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 
      // send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      // close streams 
      Log.e("Debug","File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 
     } 
     catch (MalformedURLException ex) 
     { 
       Log.e("Debug", "error: " + ex.getMessage(), ex); 
     } 
     catch (IOException ioe) 
     { 
       Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
     } 
     //------------------ read the SERVER RESPONSE 
     try { 
       inStream = new DataInputStream (conn.getInputStream()); 
       String str; 

       while ((str = inStream.readLine()) != null) 
       { 
        Log.e("Debug","Server Response "+str); 
       } 
       inStream.close(); 

     } 
     catch (IOException ioex){ 
       Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
     } 

     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 

    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 

    } 

如果我寫的AsyncTask兩個類(一個唯一的區別類內部MainActivity和一個如果我寫單獨class.java文件)是該行

的AsyncTask在MainActivity內:(此代碼工作正常)

FileInputStream fileInputStream = new FileInputStream(new File(selectedPath)); 
//calling and passing parameter like it. 
new ASync().execute(selectedPath); 

的AsyncTask在單獨的Java文件(沒有工作)

FileInputStream fileInputStream = new FileInputStream(new File(path.toString())); 
    //calling and passing parameter like it. 
     AudioSync sync = new AudioSync(); 
     sync.execute(getPath(selectedPath); 

可能-b參數傳遞的問題。我用正確的方式使用FileInputStream,我是否正確傳遞參數?請參閱代碼片段。謝謝 請告訴我如何通過在單獨的類中聲明來使此代碼工作。

+0

爲什麼不傳遞一個簡單的字符串路徑作爲你的asynctask參數。將路徑保存在一個字符串中,並檢查路徑是否爲空然後通過else顯示一些無效的路徑消息。 – Ranjit

+0

如果使用單獨的java類,則還可以將參數傳遞給構造函數 –

+0

@hello檢查我的答案。你可以這樣實現 –

回答

1

傳遞參數成AsyncTask構造,如:

new ASync(selectedPath).execute(selectedPath); 

和實現AsyncTask這樣的:

public class ASync extends AsyncTask<String, Void, String> 
{ 
String pathstr=""; 

public ASync(String str){ 
    this.pathstr=str; 
    } 

} 
+0

非常感謝 – hellosheikh

+0

@hellosheikh它的工作。 –

+0

@M D是的 – hellosheikh

0

,因爲它的訪問路徑走錯了路,您的代碼不起作用。

doInBackground(Params... params)使用varargs。所以基本上這個方法是接受一個數組,而不是一個單一的變量。

如果您的方法是doInBackground(String... paths),則可以使用paths[0]獲取第一個(有時是唯一的)路徑。您可能需要先檢查paths是否爲null