Show progress while uploading image to server using MultipartEntity

I am posting an image to the server using this code:

public void loadImage(final File image) {
    new Thread(new Runnable() {
        public void run() {
            try{
                MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                entity.addPart("image", new FileBody(image));
                HttpResponse response=sendMultipart("http://myurl.net", entity);
            }catch(Exception e){
            }
        }
    }).start();
}

public HttpResponse sendMultipart(final String URL,MultipartEntity entity) throws IOException{
    HttpPost httpPost=new HttpPost(URL);
    httpPost.setEntity(entity);
    return sendRequest(httpPost);
}

public HttpResponse sendRequest(final HttpRequestBase mhttp) throws IOException{
    HttpParams params=new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, REST.CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, REST.SERVER_TIMEOUT);
    HttpClient httpClient=new DefaultHttpClient(params);
    HttpResponse response=httpClient.execute(mhttp);

    //if we're been redirected then try to get relocated page
    if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
        Header [] headers=response.getHeaders("Location");
        if(headers!=null && headers.length!=0){
            String newUrl=headers[headers.length-1].getValue();
            HttpGet httpGet=new HttpGet(newUrl);
            response=httpClient.execute(httpGet);
        }
    }


    return response;
}

      

The question is: how do I make the download progress?

Please note that this question is NOT "how to show progress", "how to upload an image". I figured it was better to use AsyncTask, but I still don't know how to make progress.

+3


source to share





All Articles