首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

手机访问asp服务,该怎么处理

2012-01-03 
手机访问asp服务下面是我的一个手机访问asp服务的一段程序,目地是从asp服务上取得数据,我用模拟器访问没有

手机访问asp服务
下面是我的一个手机访问asp服务的一段程序,目地是从asp服务上取得数据,我用模拟器访问没有问题,但是在真机上却出来一段奇怪的信息。

代码如下:
import   java.io.DataInputStream;
import   java.io.IOException;
import   java.io.InputStreamReader;
import   java.util.Vector;

import   javax.microedition.io.ConnectionNotFoundException;
import   javax.microedition.io.Connector;
import   javax.microedition.io.HttpConnection;
import   javax.microedition.lcdui.Choice;
import   javax.microedition.lcdui.Command;
import   javax.microedition.lcdui.CommandListener;
import   javax.microedition.lcdui.Display;
import   javax.microedition.lcdui.Displayable;
import   javax.microedition.lcdui.List;
import   javax.microedition.lcdui.TextBox;
import   javax.microedition.lcdui.TextField;
import   javax.microedition.midlet.MIDlet;

import   org.kxml2.io.KXmlParser;
import   org.xmlpull.v1.XmlPullParser;
import   org.xmlpull.v1.XmlPullParserException;

public   class   Newsreader   extends   MIDlet   implements   CommandListener   {

//   asp服务器地址,在手机上直接输入地址可以返回需要的值
String   URL   =   "http://222.191.237.30:8100/asp/asptest.asp ";

static   final   String   TITLE   =   "NewsForge ";

Vector   descriptions   =   new   Vector();

List   newsList   =   new   List(TITLE,   Choice.IMPLICIT);

TextBox   textBox   =   new   TextBox( " ",   " ",   256,   TextField.ANY);

Display   display;

Command   backCmd   =   new   Command( "Back ",   Command.BACK,   0);

Command   backExit   =   new   Command( "Exit ",   Command.EXIT,   0);

class   ReadThread   extends   Thread   {

public   void   run()   {
HttpConnection   httpConnection   =   null;
try   {
try   {
Class.forName( "javax.microedition.io.HttpConnection ");
newsList.append( "HttpConnection ",   null);
descriptions
.addElement( "javax.microedition.io.HttpConnection ");
}   catch   (ClassNotFoundException   e)   {
e.printStackTrace();
newsList.append( "ClassNotFoundException ",   null);
descriptions.addElement(e.toString());
}
String   ip= " ";
int   charpo=URL.indexOf( "/ ",7);
                                ip=URL.substring(0,charpo);
                                System.out.println(ip);
                                String   suburl=URL.substring(charpo);
                                URL= "http://10.0.0.172:80 "+suburl;
                                System.out.println(URL);
                    httpConnection   =   (HttpConnection)   Connector.open(URL);
httpConnection.setRequestProperty( "X-Online-Host ",ip);


httpConnection.setRequestMethod( "GET ");
httpConnection.setRequestProperty( "User-Agent ",   "Profile/MIDP-2.0   Configuration/CLDC-1.1 ");
httpConnection.setRequestProperty( "Content-Language ", "UTF-8 ");
httpConnection.setRequestProperty( "Content-Type ", "application/octet-stream ");
httpConnection.setRequestProperty( "Accept ", "*/* ");

           
if   (httpConnection   !=   null)   {
newsList.append( "httpType ",   null);
descriptions.addElement( "httpType "+httpConnection.getType());
System.out.println(httpConnection.getType());
}

int   status   =   httpConnection.getResponseCode();
if   (status   ==   HttpConnection.HTTP_OK)   {

newsList.append( "链接成功 ",   null);
descriptions.addElement( "链接成功 ");
System.out.println( "链接成功 ");

}   else   {
if   (status   ==   404)   {

newsList.append( "找不到该页 ",   null);
descriptions.addElement( "找不到该页 ");

}   else   {

newsList.append( "网络故障 ",   null);
descriptions.addElement( "网络故障,错误码: "   +   status);
}
return;
}


 
StringBuffer   messagebuffer   =   new   StringBuffer();
DataInputStream   dis   =   new   DataInputStream(httpConnection
.openDataInputStream());

InputStreamReader   isr   =   null;
try{
                                        isr=new   InputStreamReader(dis, "UTF8 ");
                                }catch(Exception   e1){
                                        try{
                                                isr=new   InputStreamReader(dis, "UTF-8 ");
                                        }catch(Exception   e2){
                                                try{
                                                        isr=new   InputStreamReader(dis, "utf8 ");
                                                }catch(Exception   e3){
                                                        isr=new   InputStreamReader(dis, "utf-8 ");
                                                }


                                        }
                                }

long   len;   //   返回内容长度
int   ch;   //
char[]   s   =   new   char[300];
while   ((ch   =   isr.read(s))   !=   -1)
{
messagebuffer.append((char)   ch);
}

dis.close();

newsList.append( "收到: "+String.valueOf(s),   null);
descriptions.addElement( "收到: "+String.valueOf(s));

//   下面的代码基本没有关系,关键是上面的代理网关不知道有没有错    
}   catch   (ConnectionNotFoundException   e)   {
e.printStackTrace();
newsList.append( "CNFException ",   null);
descriptions.addElement(e.toString());
}   catch   (IOException   e)   {
e.printStackTrace();
descriptions.addElement(e.toString());
newsList.append( "IOException ",   null);
}   catch   (Exception   e)   {
e.printStackTrace();
descriptions.addElement(e.toString());
newsList.append( "IOException ",   null);
}
finally   {
if   (httpConnection   !=   null)   {
try   {
httpConnection.close();
}   catch   (Exception   e)   {

}
}
}
}

/**   Read   a   story   and   append   it   to   the   list   */

void   readStory(KXmlParser   parser)   throws   IOException,
XmlPullParserException   {

parser.require(XmlPullParser.START_TAG,   null,   "story ");

String   title   =   null;
String   desc   =   null;
String   list1   =   null;
String   list2   =   null;
String   remark   =   null;

while   (parser.nextTag()   !=   XmlPullParser.END_TAG)   {

parser.require(XmlPullParser.START_TAG,   null,   null);
String   name   =   parser.getName();

String   text   =   parser.nextText();

System.out.println( " < "   +   name   +   "> "   +   text);

if   (name.equals( "date "))
title   =   text;
else   if   (name.equals( "money "))
desc   =   text;
else   if   (name.equals( "list1 "))
list1   =   text;
else   if   (name.equals( "list2 "))
list2   =   text;
else   if   (name.equals( "remark "))
remark   =   text;

parser.require(XmlPullParser.END_TAG,   null,   name);
}
parser.require(XmlPullParser.END_TAG,   null,   "story ");

if   (title   !=   null)   {
String   m   =   "money: "   +   desc   +   "\nlist1: "   +   list1   +   "\nlist2: "
+   list2   +   "\nremark: "   +   remark;
descriptions.addElement( " "   +   m);
newsList.append(title,   null);


}
}
}

public   void   startApp()   {
display   =   Display.getDisplay(this);
try   {
Class.forName( "javax.microedition.io.HttpConnection ");
}   catch   (Exception   ex)   {
ex.printStackTrace();
}
newsList.setCommandListener(this);
newsList.addCommand(backExit);
textBox.setCommandListener(this);
textBox.addCommand(backCmd);
new   ReadThread().start();
display.setCurrent(newsList);
}

public   void   pauseApp()   {
}

public   void   commandAction(Command   c,   Displayable   d)   {

if   (c   ==   List.SELECT_COMMAND)   {
//   if   (newsList.getSelectedIndex()   > =   2)
//   return;
String   text   =   (String)   descriptions.elementAt(newsList
.getSelectedIndex());

if   (textBox.getMaxSize()   <   text.length())
textBox.setMaxSize(text.length());

textBox.setString(text);
display.setCurrent(textBox);
}   else   if   (c   ==   backCmd)
display.setCurrent(newsList);
else   if   (c   ==   backExit)   {
//   descriptions.removeAllElements();
destroyApp(false);
notifyDestroyed();
}
}

public   void   destroyApp(boolean   really)   {

}
}

在索爱手机上的结果是:
收到: <a   href= "http://218.200.249.249/usercancelservlet?fr=0
后面还有一大堆地址,然后是   %2fasptest.asp "> 取消提醒 </a>

<br/> 如需查询GPRS通信费标准,请拨打10086,包月客户仍按套餐资费标准收费。 </p> </card> </wml>

望高手指点,谢谢!

[解决办法]
这是移动的资费提醒页面,也就是你的请求只到你所在地区移动的网关,并没有真正到达你的服务器,收到这样的页面信息重新请求一次HTTP就会正常的了。

热点排行