I am using AsyncTask for sending files to the server.the Asynctask is a different class file not inside the MainActivity.the problem what i am facing is when i write AsyncTask inside the MainActivity class. everything works great. but when i write different java class and send parameter by accessing this class from MainActivity i get these errors
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)
here is my code
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) {
}
}
the only difference between two classes (one if i write asyncTask class inside MainActivity and one if i write seperate class.java file) is this line
AsyncTask inside the MainActivity:(This code works fine)
FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
//calling and passing parameter like it.
new ASync().execute(selectedPath );
AsyncTask in separate Java file(not working)
FileInputStream fileInputStream = new FileInputStream(new File(path.toString()) );
//calling and passing parameter like it.
AudioSync sync = new AudioSync();
sync.execute(getPath(selectedPath);
might b parameter passing issue. Am i using FileInputStream
in the right way and am i passing the parameters correctly? Please see the code snippet. thanks
please do tell me how can i make this code work by declaring in separate class.
Pass Parameter into constructor of AsyncTask
like:
new ASync(selectedPath).execute(selectedPath);
And implement your AsyncTask
like:
public class ASync extends AsyncTask<String, Void, String>
{
String pathstr="";
public ASync(String str){
this.pathstr=str;
}
}