日期:2014-05-20  浏览次数:20674 次

急!(悬赏高分)j2me获取网页数据。
J2ME 获取网络数据

  运行j2me程序, 后台访问网页,获取网页中的每一行中文并存在数组里 
   
 最后通过 g.drawString 将这一串中文数组 显示到手机屏幕上 。 (网页格式UTF-8)


很急 很急!!在线等!!!
 

------解决方案--------------------
public String requestGET(String URLString,String URL) throws IOException
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;
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; 

}


这个应该可以吧
------解决方案--------------------
关键是现在不知道你整个网页中除了这个天气外还有什么别的标签?有别的img吗?有别的诸如“/>/”这样的吗?如果没有的话我可以举个例子,你根据你的实际情况,改变对字符串的截取
Java code

public class TestStr {
    public static void main(String args[]){
        String string = "<img src='imag/zhenyu.png'/>~ <img src='imag/yin.png'/>/阵雨转阴 <br /> 气温:30 ~ 22 ℃ <br />";
        String temperature;
        String weather;
        int i;
        int j;
        i = string.indexOf("<br />");
        j = string.indexOf("/>/");
        weather = string.substring(j+"/>/".length(), i-1);
        System.out.println(weather);
        string = string.substring(i+"<br />".length(),string.length());
        i = string.indexOf("<br />");
        temperature = string.substring(" 气温:".length(),i-1);
        System.out.println(temperature);
    }
}