开发者

how to know HTTP Post upload complete while using HttpResponse in android

开发者 https://www.devze.com 2023-04-10 07:28 出处:网络
Hi iam writing a sample application where i need to select a image from gallery and upload the image to server using HTTP post.

Hi iam writing a sample application where i need to select a image from gallery and upload the image to server using HTTP post.

my code is as shown

file = new File(mSelectedFileName);

Toast tt4 = Toast.makeText(getApplicationContext(), "in upload", Toast.LENGTH_LONG);

tt4.show();

try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(url);

    InputStreamEntity reqEntity = new InputStreamEntity(
            new FileInputStream(file), -1);
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(true); // Send in multiple parts if needed
    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    //Do something with response...

开发者_JAVA百科
} catch (Exception e) {
    // show error
    Toast tt2 = Toast.makeText(getApplicationContext(), ""+e.getMessage(), Toast.LENGTH_LONG);

    tt2.show();
}

here while upload is in progress i need to show a progress bar. and once complete in need to stop the progress bar. how can i get to know the upload is complete.


You should use AsyncTask to do this because doing it on the UI thread is undesirable. There are many examples of this just search [SO] for

"[android] asyncTask progressbar http"

Here's an example I found on the web as an example create-a-custom-progress-bar-using-asynctask which has more links to look at. Also loads of exacmples on [SO]

Basically you just need to override the AsyncTask class.

0

精彩评论

暂无评论...
验证码 换一张
取 消