Partage de codes sources - CodesWall.info
Java
> test
Titre
test
Postée le
30-03-2009
Affichée
515
Mini-lien
Description
test
Etat
Ne contient pas d'erreurs.
Code d'insertion
Options
Tweet
Téléchargement
/**
* HTTP.java
*
* Created on 18 March 2005, 15:18
*/
package
HTTP
;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
/**
* An example MIDlet which demonstrates the use of multi-threading
* with an HTTP connection in one thread and the MIDlet in the main
* thread.
*
* @author Motocoder
* @version 1.0
*/
public
class
HTTP
extends
MIDlet
implements
CommandListener
{
private
final
int
BUFSIZE =
8192
;
// HTML raw data Buffer size
private
byte
databuf
[
]
=
new
byte
[
BUFSIZE
]
;
// HTTP raw data buffer
private
Display display
;
// The display object
private
Form form
;
// The Form UI object
private
HttpThread hthread
;
// The HTTP thread object
private
int
urlcnt
;
// The URL count pointer
private
Command exitCmd
;
// The Exit Command object
private
String
urls
[
]
=
{
"http://www.motorola.com"
,
"http://www.motocoder.com"
}
;
// The URLs to be accessed
public
HTTP
(
)
{
// initialise objects
form =
new
Form
(
"HTTPThread"
)
;
display = Display.
getDisplay
(
this
)
;
exitCmd =
new
Command
(
"Exit"
,
1
, Command.
EXIT
)
;
form.
addCommand
(
exitCmd
)
;
form.
setCommandListener
(
this
)
;
// Initialise and start the HTTP reader thread
hthread =
new
HttpThread
(
)
;
hthread.
start
(
)
;
// set the display
display.
setCurrent
(
form
)
;
}
public
void
startApp
(
)
{
}
public
void
pauseApp
(
)
{
}
public
void
destroyApp
(
boolean
unconditional
)
{
hthread.
killThread
(
)
;
notifyDestroyed
(
)
;
}
public
void
commandAction
(
Command c, Displayable d
)
{
if
(
c == exitCmd
)
{
destroyApp
(
true
)
;
}
}
// The HTTP reader thread inner class
class
HttpThread
extends
Thread
{
private
boolean
running
;
public
HttpThread
(
)
{
running =
true
;
urlcnt =
0
;
}
private
void
getUrl
(
String
url
)
{
HttpConnection conn =
null
;
InputStream
is =
null
;
ByteArrayOutputStream
bos =
null
;
int
rescode =
0
;
int
len =
0
;
int
readlen =
0
;
try
{
bos =
new
ByteArrayOutputStream
(
)
;
form.
append
(
"Connecting to "
+ url +
"
\n
"
)
;
conn =
(
HttpConnection
)
Connector.
open
(
url
)
;
form.
append
(
"Opening inputstream
\n
"
)
;
is = conn.
openInputStream
(
)
;
form.
append
(
"Reading responsecode
\n
"
)
;
rescode = conn.
getResponseCode
(
)
;
form.
append
(
"responsecode: "
+ rescode +
"
\n
"
)
;
len =
(
int
)
conn.
getLength
(
)
;
form.
append
(
"Reading data "
+ len +
" bytes
\n
"
)
;
if
(
len ==
-1
)
{
readlen = is.
read
(
databuf,
0
, BUFSIZE
)
;
while
(
readlen
!
=
-1
)
{
bos.
write
(
databuf,
0
, readlen
)
;
readlen = is.
read
(
databuf,
0
, BUFSIZE
)
;
}
}
else
{
readlen = is.
read
(
databuf,
0
, BUFSIZE
)
;
int
totread = readlen
;
while
(
totread
!
= len
)
{
bos.
write
(
databuf,
0
, readlen
)
;
readlen = is.
read
(
databuf,
0
, BUFSIZE
)
;
if
(
readlen ==
-1
)
{
// An error has occurred while reading, break
// while loop and signal to application of the //error
statement
// Don't forget to close the streams and
// connections
}
else
{
totread += readlen
;
}
}
}
}
catch
(
IOException
ioe
)
{
System
.
out
.
println
(
ioe.
toString
(
)
)
;
}
finally
{
form.
append
(
"Closing connection
\n
"
)
;
if
(
is
!
=
null
)
{
try
{
is.
close
(
)
;
}
catch
(
IOException
ioe
)
{
form.
append
(
"Error closing inputstream
\n
"
)
;
form.
append
(
ioe.
toString
(
)
+
"
\n
"
)
;
}
}
if
(
conn
!
=
null
)
{
try
{
conn.
close
(
)
;
}
catch
(
IOException
ioe
)
{
form.
append
(
"Error closing connection
\n
"
)
;
form.
append
(
ioe.
toString
(
)
+
"
\n
"
)
;
}
}
}
}
public
void
killThread
(
)
{
running =
false
;
}
public
void
run
(
)
{
while
(
running
)
{
try
{
getUrl
(
urls
[
urlcnt
]
)
;
urlcnt++
;
if
(
urlcnt == urls.
length
)
{
urlcnt =
0
;
}
}
catch
(
Throwable
t
)
{
form.
append
(
t.
toString
(
)
+
"
\n
"
)
;
}
try
{
sleep
(
10000
)
;
}
catch
(
Throwable
t
)
{
form.
append
(
t.
toString
(
)
+
"
\n
"
)
;
}
}
}
}
}
Postez votre code-source
Informations
Catégorie *
-
Php
javascript
CSS
HTML
Xml
Perl
C
C++
Java
VB.Net
Script Batch
C#.Net
Visual Basic
VBA
Shell
mIRC
Latex
OCaml
pascal
MySQL
MatLab
Fichier .ini
SmallTalk
Autre
Titre *
(50 car. max)
Description *
(200 car. max)
Le code
Fonctionne
Ne fonctionne pas
Je ne sais pas
Source
* : Champs obligatoires
Fermer
Accueil
Poster
Actualités
Règles
Contact
Partenaires
Infos / Aide
Php
(58)
javascript
(35)
CSS
(20)
HTML
(16)
Xml
(17)
Perl
(11)
C
(64)
C++
(51)
Java
(48)
VB.Net
(5)
Script Batch
(8)
C#.Net
(4)
Visual Basic
(6)
VBA
(5)
Shell
(10)
mIRC
(3)
Latex
(6)
OCaml
(4)
pascal
(9)
MySQL
(13)
MatLab
(3)
Fichier .ini
(5)
SmallTalk
(1)
Autre
(17)
\n
Forum informatique