javax.microedition.io.*;
public string requestget(string urlstring,string url) throws ioexception{
// =============================================================
// urlstring是http地址,url为后面的参数
// 这里的例子是发送用的用户名和密码到服务器端进行用户验证
// 比如 string urlstring = "http://192.168.0.1:8080/login.jsp";
// string url = "?name="+this.txtname+"&pass="+this.txtpass
// =============================================================
httpconnection hpc = null;
datainputstream dis = null;
boolean newline = false;
string content = "";
try{
// ===========================================================
// 建立连接
// ===========================================================
hpc = (httpconnection)connector.open(urlstring+url);
hpc.setrequestmethod(httpconnection.get);
dis = new datainputstream(hpc.openinputstream());
int character;
// ===========================================================
// 读取返回的http内容
// ===========================================================
while((character = dis.read()) != -1){
if((char)character == '//'){
newline = true;
continue;
}
else{
if((char)character =='n'&& newline){
content +="/n";
newline = false;
}
else if(newline){
content +="//" +(char)character;
newline = false;
}
else{
content +=(char)character;
newline = false;
}
}
}
}
catch(ioexception e){
system.out.print("error:"+e);
}
finally{
if(hpc != null){
hpc.close();
hpc = null;
}
if(dis != null){
dis.close();
}
}
// ===============================================================
// 由于内容可能有中文,所以在接受到信息后要对内容进行字符集的转换
// ===============================================================
content = (unicodetogb2312(content)).trim();
return content;
}
public static string unicodetogb2312(string s){
if (s==null){ return ""; }
if (s.equals("")){ return s; }
try{
return new string(s.getbytes("iso8859_1"),"gb2312");
}
catch(exception uee){
return s;
}
}
以上就是一个简单的http连接并且从服务器获取响应信息的例子,应该很简单了吧。客户端就是上面那个样子,服务器端只要配置好iis,增加一个网页来对客户端的请求做出响应就行了,其实跟一般的网页请求没有多大区别,很简单吧!!
上面的socket客户端连接程序就应该算是完工了,下面是搭建服务器端对客户端的连接进行响应。建立服务器端程序,只需要有以下代码即可:
socketconnection
下面还是从实例入手讲一下j2me的socket编程。
程序首先打开以ip地址为192.168.0.1:6666的socket连接,如果连接失败则抛出异常,程序结束。如果socket连接成功,则继续。
public boolean socketconn(string s) throws ioexception{
// =============================================================
// s是socket连接字符串
// 这里的例子是发送用的用户名和密码到服务器端进行用户验证
// 比如 string s = "socket://192.168.0.1:6666"
// =============================================================
private streamconnection conserver;
private string strserveraddr;
private boolean bconnected;
conserver = null;
strserveraddr = s; // 连接地址
bconnected = false; // 连接状态
try
{
conserver = (streamconnection)connector.open(strserveraddr);
}
catch(exception exception)
{
system.out.println("connect server error");
bconnected = false;
return false;
}
bconnected = true;
system.out.println("connect ok!");
return true;
}
..........
try{
// 建立端口为6666的socket服务器
serversocketconnection socketser;
socketser = (serversocketconnection)connector.open("socket://:6666");
// 等待客户端连接
socketconnection sc;
// 如有连接,则新增一个线程对连接进行处理
sc = (socketconnection)socketser.acceptandopen();
..........
while(true){
// 对sc的inputstream和outputstream进行处理
}
}
..........
protected boolean senddata(byte abyte0[])//自己替换[]
{
system.out.println("send :" + bconnected);
// 判断连接情况
if(!bconnected)
return false;
outputstream outputstream = null;
try
{
outputstream = conserver.openoutputstream();
// 写信息到outputstream中
outputstream.write(abyte0);
// 我的理解是强制送出所有已经写了的信息
outputstream.flush();
outputstream.close();
}
catch(exception exception)
{
system.out.println("send data error");
bconnected = false;
try
{
if(outputstream != null)
outputstream.close();
// 调用断开连接的函数
disconnect();
}
catch(exception exception1) { }
return false;
}
return true;
}
..............
inputstream = conserver.openinputstream();
..............
新闻热点
疑难解答
图片精选