www.RoboHobby.com
package com.robohobby.me;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
@Ads
@author
public class HttpUtils implements Runnable
{
String url = " http://www.RoboHobby.com/time.jsp " ;
HttpConnection httpConnection = null ;
InputStream inputStream = null ;
OutputStream outputStream = null ;
StringBuffer resultBuffer = new StringBuffer();
CallBacks callBacks = CallBacks.getInstance();
public void run()
{
String requetString = "" ;
callBacks.robotMIDlet.getUrlInfoStringItem().setText(
" url= " + url
+ " \r \n " );
callBacks.robotMIDlet.getInfoStringItem().setText( " started \r \n " );
try
{
String targetUrl = this .url;
getHttpData( targetUrl );
String resultStr = resultBuffer.toString();
callBacks.robotMIDlet.getResultStringItem().setText( resultStr );
callBacks.robotMIDlet.getInfoStringItem().setText( " finished OK \r \n " );
}
catch ( IOException io_e )
{
callBacks.robotMIDlet.getInfoStringItem().setText( " Error io_e= " + io_e
+ " \r \n " );
}
catch ( Exception e )
{
callBacks.robotMIDlet.getInfoStringItem().setText( " Error e= " + e
+ " \r \n " );
}
}
@throws
void getHttpData( String targetUrl ) throws IOException
{
try {
int responseCode = -1;
boolean stop = false ;
while ( !stop )
{
httpConnection = (HttpConnection)Connector.open( targetUrl );
responseCode = httpConnection.getResponseCode();
if ( handleRedirectionResponseCode( responseCode ) )
{
stop = true ;
}
}
if ( isErrorResponseCode( responseCode ) )
{
return ;
}
if ( responseCode != HttpConnection.HTTP_OK)
{
throw new IOException( " Error responseCode= " + responseCode );
}
String contentType = httpConnection.getType();
handleContentType( contentType );
inputStream = httpConnection.openInputStream();
resultBuffer.setLength(0);
int contentLength = (int )httpConnection.getLength();
if ( contentLength > 0 )
{
byte [] data = new byte [contentLength];
int numberOfBytes = inputStream.read(data);
if ( numberOfBytes != -1 )
{
hanldeByteArray(data);
}
}
else
{
int ch;
while ((ch = inputStream.read()) != -1)
{
hanldeOneByte((byte )ch);
}
}
} finally {
if ( inputStream != null )
{
try
{
inputStream.close();
}
catch ( IOException io_e )
{
}
}
try
{
if (httpConnection != null ) {
httpConnection.close();
}
}
catch ( IOException io_e )
{
}
}
}
private boolean isErrorResponseCode( int responseCode ) throws IOException
{
boolean result = false ;
if ( (responseCode == HttpConnection.HTTP_NOT_IMPLEMENTED)
|| (responseCode == HttpConnection.HTTP_VERSION)
|| (responseCode == HttpConnection.HTTP_INTERNAL_ERROR)
|| (responseCode == HttpConnection.HTTP_GATEWAY_TIMEOUT)
|| (responseCode == HttpConnection.HTTP_BAD_GATEWAY)
)
{
if ( inputStream != null ) {
inputStream.close();
}
if ( outputStream != null ) {
outputStream.close();
}
if (httpConnection != null ) {
httpConnection.close();
}
result = true ;
}
return result;
}
private boolean handleRedirectionResponseCode( int responseCode ) throws IOException
{
boolean result = false ;
if (( responseCode == HttpConnection.HTTP_TEMP_REDIRECT )
|| ( responseCode == HttpConnection.HTTP_MOVED_TEMP )
|| ( responseCode == HttpConnection.HTTP_MOVED_PERM) )
{
url = httpConnection.getHeaderField(" location " );
httpConnection.close();
System.out.println(" Do redirection to " + url);
}
else
{
result = true ;
}
return result;
}
@param
void handleContentType(String contentType)
{
}
@param
void hanldeOneByte(byte b) {
resultBuffer.append((char )b);
}
@param
void hanldeByteArray(byte [] b)
{
for (int i = 0; i < b.length; i++) {
hanldeOneByte( b[i] );
}
}
public String sendDataPages( String URL , byte [] bArray, int pageSize )
{
String retStr = "" ;
int endPage = bArray.length/pageSize;
for ( int i = 0; i <= endPage; i++ )
{
try {
String data = doEncode(bArray, i * pageSize, (i + 1) * pageSize);
String params = " &p= " + i + " &e= " + endPage + " &s= "
+ bArray.length + " &d= " + data + " &tmp=T " ;
callBacks.robotMIDlet.getUrlInfoStringItem().setText(" sendDataPages()
params= " + " &p= "
+ i + " &e= "
+ endPage + " &s= " + bArray.length);
String targetUrl = this .url;
getHttpData( targetUrl + params );
retStr = this .resultBuffer.toString();
} catch (IOException io_e) {
retStr = "" +io_e;
}
}
return retStr;
}
public String doEncode( byte [] bArray, int start, int end )
{
if ( bArray != null )
{
StringBuffer tmp = new StringBuffer();
for ( int i = start ; i < end && i < bArray.length ; i++ )
{
tmp.append( "" + bArray[i] + " ; " );
}
return tmp.toString();
}
return null ;
}
}
Contact us: