HTTPClient and Progressbar.

Discussion in 'Java' started by crazytazo, Nov 11, 2008.

  1. crazytazo

    crazytazo Guest

    PostMethod filePost = new PostMethod(ui.value.getStrEIP());
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE,
    true);

    try {
    Part[] parts = {
    new StringPart("FILENAME", file.m_File.getName()),
    new StringPart("DIRFLAG", String.valueOf(0)),
    new StringPart("FILEINDEX", String.valueOf(jfidx)),
    new StringPart("FILESAVEPATH", ui.value.getStrPath()),
    new FilePart(file.m_File.getName(), file.m_File)
    };

    filePost.setRequestEntity(
    new MultipartRequestEntity(parts,
    filePost.getParams()));

    HttpClient client = new HttpClient();

    client.getHostConfiguration().setHost(ui.value.getStrEIP(), 80);
    client.getHttpConnectionManager().
    getParams().setConnectionTimeout(5000);

    int status = client.executeMethod(filePost);

    if (status == HttpStatus.SC_OK) {
    System.out.println("Upload complete, response=" +
    filePost.getResponseBodyAsString());
    } else {
    System.out.println("Upload failed, response=" +
    HttpStatus.getStatusText(status));
    }

    } catch (Exception ex) {
    System.out.println("ERROR: " + ex.getClass().getName() + "
    " + ex.getMessage());
    ex.printStackTrace();
    } finally {
    filePost.releaseConnection();
    }

    this is client code using apache.HTTPClient.

    it's work well, but follow line is occur blocking until get response
    code from server.
    int status = client.executeMethod(filePost);

    How to check writing bytes in this modules?
    The bytes are need to represnt progressbar.
     
    crazytazo, Nov 11, 2008
    #1
    1. Advertising

  2. In article
    <>,
    crazytazo <> wrote:

    [...]
    > t work well, but [the] follow[ing] line block
    > until get [a] response code from server.
    >
    > int status = client.executeMethod(filePost);
    >
    > How [should I] check writing bytes in this module?
    > The bytes are need[ed] to repres[e]nt progress.


    Consider using a SwingWorker:

    <http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
    <http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html>

    There's a nice prime number example here with a JProgressBar:

    <https://swingworker.dev.java.net/>

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Nov 11, 2008
    #2
    1. Advertising

  3. crazytazo

    rabbits77 Guest

    > it's work well, but follow line is occur blocking until get response
    > code from server.
    > int status = client.executeMethod(filePost);


    This is not what you seem to think it is.
    status is a http response code and is, of course,
    is blocking.
    Here is a list of response codes if you are interested
    http://kbs.cs.tu-berlin.de/~jutta/ht/responses.html

    > How to check writing bytes in this modules?
    > The bytes are need to represnt progressbar.


    One way to do this is shown on this page which
    happens to cover performance optimization
    http://hc.apache.org/httpclient-3.x/performance.html
    In particular I think something like this would work

    HttpClient httpclient = new HttpClient();
    GetMethod httpget = new GetMethod("http://www.myhost.com/");
    try {
    httpclient.executeMethod(httpget);
    Reader reader = new InputStreamReader(
    httpget.getResponseBodyAsStream(),
    httpget.getResponseCharSet());

    /*get the response byte by byte and update your progress bar
    after each response
    */
    } finally {
    httpget.releaseConnection();
    }
     
    rabbits77, Nov 11, 2008
    #3
  4. crazytazo

    crazytazo Guest

    On Nov 12, 3:29 am, rabbits77 <> wrote:
    > > it's work well, but follow line is occur blocking until get response
    > > code from server.
    > >  int status = client.executeMethod(filePost);

    >
    > This is not what you seem to think it is.
    > status is a http response code and is, of course,
    > is blocking.
    > Here is a list of response codes if you are interestedhttp://kbs.cs.tu-berlin.de/~jutta/ht/responses.html
    >
    > > How to check writing bytes in this modules?
    > > The bytes are need to represnt progressbar.

    >
    > One way to do this is shown on this page which
    > happens to cover performance optimizationhttp://hc.apache.org/httpclient-3.x/performance.html
    > In particular I think something like this would work
    >
    >    HttpClient httpclient = new HttpClient();
    >    GetMethod httpget = new GetMethod("http://www.myhost.com/");
    >    try {
    >      httpclient.executeMethod(httpget);
    >      Reader reader = new InputStreamReader(
    >              httpget.getResponseBodyAsStream(),
    > httpget.getResponseCharSet());
    >
    >      /*get the response byte by byte and update your progress bar
    >        after each response
    >        */
    >    } finally {
    >      httpget.releaseConnection();
    >    }


    Thanks rabbits77.

    Your code is GET method.
    Problem is POST method.

    I have solved this problem by override FilePart.sendData(OutputStream
    out)
     
    crazytazo, Nov 12, 2008
    #4
  5. crazytazo

    crazytazo Guest

    On Nov 11, 11:13 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >
    >  crazytazo <> wrote:
    >
    > [...]
    >
    > > t work well, but [the] follow[ing] line block
    > > until get [a] response code from server.

    >
    > >  int status = client.executeMethod(filePost);

    >
    > > How [should I] check writing bytes in this module?
    > > The bytes are need[ed] to repres[e]nt progress.

    >
    > Consider using a SwingWorker:
    >
    > <http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
    > <http://java.sun.com/docs/books/tutorial/uiswing/concurrency/worker.html>
    >
    > There's a nice prime number example here with a JProgressBar:
    >
    > <https://swingworker.dev.java.net/>
    >
    > --
    > John B. Matthews
    > trashgod at gmail dot comhttp://home.roadrunner.com/~jbmatthews/


    Thanks John.
    It's very good!
     
    crazytazo, Nov 12, 2008
    #5
  6. crazytazo

    Roedy Green Guest

    On Tue, 11 Nov 2008 13:29:01 -0500, rabbits77 <>
    wrote, quoted or indirectly quoted someone who said :

    > /*get the response byte by byte and update your progress bar
    > after each response
    > */


    you could also read whatever is available then update progress.

    see http://mindprod.com/product1.html#HTTP
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    Your old road is
    Rapidly agin'.
    Please get out of the new one
    If you can't lend your hand
    For the times they are a-changin'.
     
    Roedy Green, Nov 19, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maka Sili
    Replies:
    0
    Views:
    517
    Maka Sili
    Aug 25, 2004
  2. Nilton Volpato
    Replies:
    0
    Views:
    398
    Nilton Volpato
    May 8, 2006
  3. Nilton Volpato
    Replies:
    0
    Views:
    450
    Nilton Volpato
    May 7, 2006
  4. OMS
    Replies:
    2
    Views:
    5,953
  5. Michael Gebhart

    Refreshing GUI and Progressbar

    Michael Gebhart, Apr 11, 2005, in forum: Ruby
    Replies:
    2
    Views:
    175
    Guillaume Cottenceau
    Apr 11, 2005
Loading...

Share This Page