当前位置: 代码迷 >> J2ME >> J2ME:如何小弟我的客户端连不上服务器呢
  详细解决方案

J2ME:如何小弟我的客户端连不上服务器呢

热度:1070   发布时间:2016-04-17 22:04:45
J2ME:怎么我的客户端连不上服务器呢?
我做的是一个基于手机的信息查询系统,在客户端连接服务器端时,怎么也连不上,请各位大虾点拨,重分礼谢了!!以下是我的程序:
客户端:
HttpConnection   conn   =   null;

InputStream   iStrm   =   null;

String   url   =   "http://127.0.0.1:8080/DatabaseServer/server/DatabaseServlet ";

try   {
//创建httpconnection   连接
conn   =   (HttpConnection)   Connector.open(url);
conn.setRequestMethod(HttpConnection.POST);

//设置连接属性
conn.setRequestProperty( "User-Agent ",
"Profile/MIDP-2.0   Configuration/CLDC-1.1 ");
conn.setRequestProperty( "Content-Language ",   "en-US ");
conn.setRequestProperty( "Content-Type ",   "application/octet-stream ");
conn.setRequestProperty( "Accept ",   "application/octet-stream ");
conn.setRequestProperty( "Connection ",   "Keep-Alive ");


if   (conn.getResponseCode()   ==   HttpConnection.HTTP_OK)   {

iStrm   =   conn.openInputStream();
DataInputStream   dis   =   new   DataInputStream(iStrm);

//获得数据信息
int   length   =   (int)   conn.getLength();

if   (length   >   0)   {
//读取传输过来的问题的数目
int   num   =   dis.readInt();
System.out.println(num);

String   question;
for(int   i   =   0;i   <   num;i++){
//读取问题
question   =   dis.readUTF();
//显示返回信息
form.append(question);
}

}   else
form.append( "不能访问数据!111111111111111 ");
}else
form.append( "不能访问数据!22222222222222222222 ");



}   catch   (Exception   e)   {
form.append( "网络出错 ");
System.out.println(e.toString());
}   finally   {
//关闭连接对象
if   (iStrm   !=   null)
iStrm.close();
if   (conn   !=   null)
conn.close();

}
dis.setCurrent(form);

服务器端:
response.setContentType( "application/ocet-stream ");
InputStream   is=request.getInputStream();
OutputStream   os=response.getOutputStream();

DataInputStream   call=new   DataInputStream(is);
DataOutputStream   result=new   DataOutputStream(os);
DBConnetion   conn   =   new   DBConnetion();/*DBConnetion是我自己写的一个连接数据库的类,我测试过没有错,我数据库是sql2000,程序是在Eclispe3.2上开发的。*/
        ResultSet   rs1   =   conn.ExecuteSQL( "select   *   from   loginTable ",   0);
                        if(rs1.next()){
                        String   str   =   rs1.getString(1);
                        result.writeUTF(str);
                        System.out.println(str);
                        str   =   rs1.getString(2);
                        result.writeUTF(str);
                        System.out.println(str);
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!