?? connection.java.svn-base
字號:
package wFramework;
import javax.microedition.io.*;
import javax.microedition.lcdui.Image;
import java.io.IOException;
import java.io.InputStream;
/**
* Simple threaded class used to connect to HTTP servers.
* @author Marius Bj鴕ge
*
*/
public class Connection extends Thread
{
private ConnectionListener listener;
private String url;
private String result;
private Object param;
private boolean working;
private boolean getimage;
private Image img;
/**
* Constructor. Receives a connection listener.
* @param listener
*/
public Connection(ConnectionListener listener)
{
this.listener = listener;
working = false;
}
public Connection()
{
listener = null;
working = false;
}
/**
* Opens URL for asynchronous communication. Result data will be output to listener.
* @param url
* @param param
*/
public void openURLAsync(String url, Object param)
{
getimage = false;
this.url = url;
this.param = param;
start();
}
public void openURLAsync(String url)
{
getimage = false;
openURLAsync(url, null);
}
/**
* Opens url for synchronous communication. The method will stall until data is read. Use with care..
* @param url
* @return
*/
public String openURLSync(String url)
{
this.url = url;
working = true;
start();
try
{
while (working)
sleep(1);
}
catch (InterruptedException e)
{
}
return result;
}
public void getImage(String url, Object param)
{
getimage = true;
this.url = url;
this.img = null;
this.param = param;
start();
}
public void run()
{
result = "";
try
{
if (getimage)
getImageHTTP(url);
else
{
result = getViaHttpConnection(url);
if (listener != null && !working)
listener.dataSuccess(result, param);
}
}
catch (IOException e)
{
result = e.toString();
if (listener != null && !working)
listener.dataFailure(result, param);
}
working = false;
}
private void getImageHTTP(String url) throws IOException
{
HttpConnection c = null;
InputStream is = null;
int rc;
url = fixUrl(url);
try
{
c = (HttpConnection)Connector.open(url);
c.setRequestProperty("Connection", "close");
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
throw new IOException("HTTP response code: " + rc);
is = c.openInputStream();
img = Image.createImage(is);
if (listener != null)
listener.imageSuccess(img, param);
}
catch (ClassCastException e)
{
throw new IllegalArgumentException("Not an HTTP URL");
}
finally
{
if (is != null)
is.close();
if (c != null)
c.close();
}
}
private String getViaHttpConnection(String url) throws IOException
{
HttpConnection c = null;
InputStream is = null;
int rc;
String back = "";
url = fixUrl(url);
try
{
c = (HttpConnection)Connector.open(url);
// c.setRequestMethod(HttpConnection.POST);
// c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Connection", "close");
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK)
{
throw new IOException("HTTP response code: " + rc);
}
is = c.openInputStream();
// Get the ContentType
// String type = c.getType();
// Get the length and process the data
int len = (int)c.getLength();
if (len > 0)
{
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1))
{
actual = is.read(data, bytesread, len - bytesread);
back += new String(data, 0, actual);
bytesread += actual;
if (listener != null)
listener.dataProgress(bytesread, len);
}
}
else // leser ein og ein byte inntil ikkje fleire bytes
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -