J2ME通过URL访问XML文件并下载XML到本地

(叶全府)J2ME通过URL访问XML文件并下载XML到本地
2009-06-11 14:45
**
     * 网络连接
     * 固定URL的网络连接
     *
@param cmnet
     *            true走cmnet false走cmwap
     *
@return返回String类型的XML数据
    
*/
   
private String connect( boolean cmnet) {

       
try {
           
if (cmnet) {

                http
= (HttpConnection) Connector
                        .open(
http://xx.cn/t.xml );

            }
else {
                http
= (HttpConnection) Connector
                        .open(
http://10.0.0.3/t.xml );
                http.setRequestProperty(
" X-Online-Host " , " xx.cn " );
            }

            http.setRequestProperty(
" Content-Type " , " application/octet-stream " );
            http.setRequestProperty(
" Connection " , " Keep-Alive " );
            http.setRequestMethod(HttpConnection.GET);

           
// 判断是否为连接网络收费提示页面
            String content_type = http.getHeaderField( " Content-Type " );
           
// 如果是返回移动的拦截页面,则重发.
            if (content_type.indexOf( " wml " ) != - 1 ) {
               
try {
                    http.close();
                }
catch (IOException e) {
                }
                http
= null ;
               
// 重新发起一次请求

               
if (cmnet) {
                    http
= (HttpConnection) Connector
                            .open(
http://xx.cn/t.xml );
                }
else {
                    http
= (HttpConnection) Connector
                            .open(
http://10.0.0.3/t.xml );
                    http.setRequestProperty(
" X-Online-Host " ,
                           
" xx.cn " );
                }
                http.setRequestMethod(HttpConnection.GET);

            }
           
int code = http.getResponseCode();

           
if (code == 200 ) { // 返回200 表示连接成功

               
// 打开输入流,读取数据
                InputStreamReader is = new InputStreamReader(http
                        .openInputStream(),
" UTF-8 " );
               
// 建一StringBuffer,用于保存已读的数据
                StringBuffer sb = new StringBuffer();
               
// 每次读取1K的数据
                char [] c = new char [ 1024 ];
               
int k;
               
// 循环读取数据,直到读完 当返回-1时表示流已读完
                while ((k = is.read(c)) != - 1 ) {
                    sb.append(c,
0 , k);
                }

                is.close();
                is
= null ;
                http.close();
                http
= null ;
               
return sb.toString();
            }
        }
catch (Exception e) {
            e.printStackTrace();
           
// return null;
        } finally {

           
if (http != null ) {
               
try {
                    http.close();
                    http
= null ;
                }
catch (IOException e) {
                }
                http
= null ;
            }
        }

       
return null ;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值