2014-02-23 158 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