Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   HTTPClient and Progressbar. (http://www.velocityreviews.com/forums/t643943-httpclient-and-progressbar.html)

crazytazo 11-11-2008 09:46 AM

HTTPClient and Progressbar.
 
PostMethod filePost = new PostMethod(ui.value.getStrEIP());
filePost.getParams().setBooleanParameter(HttpMetho dParams.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.get StrEIP(), 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.

John B. Matthews 11-11-2008 02:13 PM

Re: HTTPClient and Progressbar.
 
In article
<d3d81f2d-2768-481c-b001-57008f20bc82@d36g2000prf.googlegroups.com>,
crazytazo <crazytazo@gmail.com> wrote:

[...]
> [I]t work[s] well, but [the] follow[ing] line block[s]
> until [I] 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/

rabbits77 11-11-2008 06:29 PM

Re: HTTPClient and Progressbar.
 
> 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();
}

crazytazo 11-12-2008 02:34 AM

Re: HTTPClient and Progressbar.
 
On Nov 12, 3:29*am, rabbits77 <rabbit...@my-deja.com> 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 11-12-2008 02:35 AM

Re: HTTPClient and Progressbar.
 
On Nov 11, 11:13*pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <d3d81f2d-2768-481c-b001-57008f20b...@d36g2000prf.googlegroups.com>,
>
> *crazytazo <crazyt...@gmail.com> wrote:
>
> [...]
>
> > [I]t work[s] well, but [the] follow[ing] line block[s]
> > until [I] 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!

Roedy Green 11-19-2008 12:02 AM

Re: HTTPClient and Progressbar.
 
On Tue, 11 Nov 2008 13:29:01 -0500, rabbits77 <rabbits77@my-deja.com>
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'.


All times are GMT. The time now is 02:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.