0

I searched many times on google and stackoverflow, but I cant find really what I need. I wrote a code which upload file to server with asynctask method. What I did is: In asyntask method I upload my file with post method to my webservice. in doInBackground method :

int serverResponseCode = 0;
try {

    // open a URL connection to the Servlet
    FileInputStream fileInputStream = new FileInputStream(sourceFile);

    // ------------------ CLIENT REQUEST
    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

    // Use a post method.
    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("file_name", fileName);
    conn.setRequestProperty("file_name_audio", fileName); 
    // conn.setFixedLengthStreamingMode(1024);
    // conn.setChunkedStreamingMode(1);

    dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);

    dos.writeBytes("Content-Disposition: form-data; name=\"file_name\";filename=\""
        + fileName + "\"" + lineEnd);

    dos.writeBytes(lineEnd);


        // create a buffer of maximum size
    bytesAvailable = fileInputStream.available();

    int streamSize = (int) sourceFile.length();
    bufferSize = streamSize / 4;

    buffer = new byte[streamSize];
    int sentBytes=0;
    // read file and write it into form...
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    while (bytesRead > 0) {  
    Thread.sleep(2);  
    sentBytes += bufferSize;
    int progress = (int)((sentBytes / (float) bytesAvailable) * 100);
    publishProgress( progress+""); 
    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);

    // Responses from the server (code and message)
    serverResponseCode = conn.getResponseCode();
    @SuppressWarnings("unused")
    String serverResponseMessage = conn.getResponseMessage();

    fileInputStream.close();  
    dos.flush();
    dos.close();
    
} catch (MalformedURLException ex) {
    ex.printStackTrace();
     Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
    e.printStackTrace();
}

String serverResponseMessage = conn.getResponseMessage();is long more time

my uploadprogress is setting 4 times and then I wait more time to really upload my file to server.Why I need if the code exit from while.How can I set progressbar value more sensivity and when progressbar value setting 100 ,I want to the file upload is finish really. I set onpreexecute and onpostexecute methods. But I think they are no need to write here. And I use notificationbar progress so I have to show percentage.

Community
  • 1
  • 1
CompEng
  • 7,161
  • 16
  • 68
  • 122

2 Answers2

0

There are several methods in AsynTask that you can override and one of them is the following:

void onPreExecute()
{
   //here you can show your progress bar
}

Then there is the other method that gets called when you are done with the task:

void onPostExecute(String result)
{
    //here you can dismiss your progressbar
}

Now, as far as I know, you could use different kinds of bars to show your progress; from simplified ones that show a circle to horizontal ones with different completion levels.

If you show the rounded one, you will not have to show percentages but if you go with horizontal one, you will have to do some calculations and set it accordingly!

Remember, you can update the progressbar from within doInBackground depending on how much work is left!

I hope this helps!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
-1

This are the steps i have implemented in one of my project, this scenario might help or not for your situation.

  • Create a runnable thread that uploads a file. This class contains a get method and we use this method to calculate the progress bar value.
  • The get method contains logic (like get the size of the file and need the how data you have uploaded, from this values you will get the percentile which u may use for progress bar).
  • Create a Timer which sets progress bar value.
  • Start the timer just before the process start
  • The timer thread which sets the progress bar value and upload thread uploads and provides progress bar value.

I have done this and it worked. I hope this might help a little.

Thanks

  • thaks for answer, I agree with you , I try to done this but somewhere there is problem, please look my code – CompEng Mar 27 '14 at 20:34