2011-03-18 48 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

相關問題