2011-03-18 49 views
0

我正在从一个应用程序中获取服务器的数据并解析xml并在列表视图中显示它。但问题是代码在模拟器上运行良好,但是当我在设备上安装应用程序时,它并未从服务器获取数据(未连接到Internet)。黑莓应用程序不通过互联网在设备上获取数据

我已经使用BB密钥签署了该应用程序,因此该部分没有错误。

下面是一些我的代码..我使用连接到互联网 -

public XMLParser() throws SAXException, IOException{ 

      // connect to feed's URL 
      String url=urlToHit; 
      System.out.println(url); 
      try { 
       httpConnection = (HttpConnection)Connector.open(url); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       inputStream = httpConnection.openDataInputStream(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK) 
      { 
       // check header field for a specific encoding 
       String desiredEncoding = "ISO-8859-1"; //iso-8859-1 
       String contenttype = httpConnection.getHeaderField("Content-Type"); 
       if (contenttype != null) 
       { 
        contenttype = contenttype.toUpperCase(); 
        if (contenttype.indexOf("UTF-8") != -1) 
        { 
         desiredEncoding = "UTF-8"; 
        } 
       } 

       // we need an input source for the sax parser 
       InputSource is = new InputSource(inputStream); 

       // setup Encoding to match what the web server sent us 
       is.setEncoding(desiredEncoding); 

       // create the factory 
       SAXParserFactory factory = SAXParserFactory.newInstance(); 

       // create a parser 
       SAXParser parser = null; 
       try { 
        parser = factory.newSAXParser(); 
       } catch (ParserConfigurationException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (SAXException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       // instantiate our handler 
       DefaultHandler myHandler= new DefaultHandler(){ 


        public void startElement(String uri, String localName,String element_name, Attributes attributes)throws SAXException{ 


         if (element_name.equals("Books")){ 
          bookCount=attributes.getValue("booksCount"); 

        } 
        if (element_name.equals("Book")){ 

         TableRowManager row = new TableRowManager(); 

         Bitmap scaledBitmap = new Bitmap(50, 70); 

         Bitmap img=Bitmap.createBitmapFromBytes((getImageFromUrl(attributes.getValue("image")).getBytes()), 0,-1, 1); 

         img.scaleInto(scaledBitmap, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT); 
         //img=(new WebBitmapField(attributes.getValue("image"))).getBitmap(); 
         row.add(new BitmapField(scaledBitmap)); 

         row.add(new LabelField(attributes.getValue("title"),DrawStyle.ELLIPSIS)); 
         //row.add(new BitmapField(attributes.getValue("image"),)); 
         LabelField lf1=new LabelField("Author:"+attributes.getValue("author"),DrawStyle.ELLIPSIS){ 

          protected void paint(Graphics graphics) { 
          graphics.setColor(0x00878787); 

          super.paint(graphics); 

          } 
         }; 

         row.add(lf1); 
         LabelField lf2=new LabelField("ISBN:"+attributes.getValue("isbn13"),DrawStyle.ELLIPSIS){ 

          protected void paint(Graphics graphics) { 
          graphics.setColor(0x00878787); 

          super.paint(graphics); 

          } 
         }; 

         row.add(lf2); 

         LabelField lf3=new LabelField("year:"+attributes.getValue("year"),DrawStyle.ELLIPSIS){ 

          protected void paint(Graphics graphics) { 
          graphics.setColor(0x00878787); 

          super.paint(graphics); 

          } 
         }; 

         row.add(lf3); 

         title.addElement(attributes.getValue("title")); 
         isbn.addElement(attributes.getValue("isbn13")); 
         bookImg.addElement(attributes.getValue("image")); 
         author.addElement(attributes.getValue("author")); 
         year.addElement(attributes.getValue("year")); 



         row.add(new BitmapField(p1)); 

         rows.addElement(row); 
        } 

        } 
        public void characters(char[] ch, int start, int len) throws SAXException{ 



        } 

       }; 

       // perform the synchronous parse   
       parser.parse(is,myHandler); 
      } 



     } 

请建议。

+1

什么黑莓特定修饰符,如果有的话,你追加到URL?该设备是否配置了BlackBerry数据计划? – Richard 2011-03-18 13:47:34

+1

你能够从BB浏览器浏览互联网吗? – Tamar 2011-03-18 13:52:34

+0

是的,我可以从BB浏览器浏览互联网。其实互联网在设备上工作正常,但该应用程序没有访问互联网。 – 2011-03-18 13:57:02

回答

3

;deviceside=true附加URL表示您正在使用DIRECT_TCP传输。在某些无线提供商处,此传输也需要在设备上配置APN设置。请注意,APN设置通常是无线提供商特定的。

使用谷歌搜索你可以找到你的无线提供商的APN设置。例如,检查这个link

+0

你可以恳请我解释一下不同的类型,如DIRECT_TCP,BIS等,以及它们是如何使用的以及在哪些条件下使用的。 – 2011-03-22 07:02:41

+1

@Dinesh Sharma:这是一个非常广泛的话题,需要在这里回答,所以我只是指向你到信息源 - http://supportforums.blackberry.com/t5/Java-Development/Connecting-your-BlackBerry-http-and-socket-connections-to-the/mp/206243#M29104 – 2011-03-22 10:52:15

相关问题