Partage de codes sources - CodesWall.info
Java
> test
Titre
test
Postée le
30-03-2009
Affichée
560
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 *
-
ActionScript3
ASP
C
C#.Net
C++
COBOL
CSS
Delphi
Fichier .ini
HTML4
HTML5
Java
javascript
jQuery
Latex
MatLab
mIRC
MySQL
Objective-C
OCaml
pascal
Perl
Php
Python
QBasic
Ruby
Script Batch
Shell
SmallTalk
VB.Net
VBA
Visual Basic
XML
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
ActionScript3
(0)
ASP
(0)
C
(65)
C#.Net
(4)
C++
(51)
COBOL
(0)
CSS
(21)
Delphi
(0)
Fichier .ini
(5)
HTML4
(16)
HTML5
(2)
Java
(48)
javascript
(35)
jQuery
(0)
Latex
(6)
MatLab
(3)
mIRC
(3)
MySQL
(13)
Objective-C
(1)
OCaml
(4)
pascal
(9)
Perl
(11)
Php
(58)
Python
(3)
QBasic
(1)
Ruby
(1)
Script Batch
(8)
Shell
(10)
SmallTalk
(1)
VB.Net
(5)
VBA
(5)
Visual Basic
(6)
XML
(17)
Autre
(18)
\n
Forum informatique