2011-05-30 134 views
0

我是黑莓手机新手。我使用黑莓9800模拟器创建HTTP连接。我写了下面的代码。HttpConnection黑莓8900

public MyScreen() 
{   
    // Set the displayed title of the screen  
    setTitle("Hello"); 
    ConnectionFactory connFact = new ConnectionFactory(); 
    ConnectionDescriptor connDesc; 
    connDesc = connFact.getConnection("http://whistlersbest.net/api/categories.php?parent_id=0"); 
    System.out.println("hello " + connFact); 
    if (connDesc != null) 
    { 
     HttpConnection httpConn; 
     httpConn = (HttpConnection)connDesc.getConnection(); 
     try 
     { 
      in = httpConn.openInputStream(); 

      int ii; 
      while((ii=in.read()) != -1){ 

       strbuffer = strbuffer + (char)ii; 
      } 
     } 
     catch (IOException e) 
     { 
      System.err.println("Caught IOException: " 
       + e.getMessage()); 
     } 
    } 
    setTitle("Whistlers Best"); 
    System.out.println("set Title"); 
    xmlHandler xm = new xmlHandler(strbuffer); 
    category = xm.getCategory(); 
    System.out.println("category set"); 
    String[] arr = new String[category.getIds().size()]; 
    System.out.println("Array " + arr.length); 
    list = new ObjectListField(){ 
     protected boolean navigationClick(int status, int time) { 
      //UiApplication.getUiApplication().pushScreen(new SubCategories(category.getIds().elementAt(list.getSelectedIndex()).toString(),"1",category.getNames().elementAt(list.getSelectedIndex()).toString())); 
      return false; 

     } 

    }; 
    for(int y = 0; y<category.getIds().size() ; y++){ 
     arr[y] = (String)category.getNames().elementAt(y); 
     System.out.println("string: "+arr[y]); 
    } 


    list.set(arr); 
    VerticalFieldManager hfm = new VerticalFieldManager(Manager.FIELD_HCENTER); 
    hfm.add(new LabelField("Categories", 
      Field.FIELD_HCENTER)); 
    hfm.add(list); 
    list.setChangeListener(this); 
    add(hfm); 

} 

它完美地工作。但突然间我需要从黑莓9800切换到黑莓8900曲线。我使用模拟器,并写入相同的代码。但我得到NullPointerException在线:

connDesc = connFact.getConnection("http://whistlersbest.net/api/categories.php?parent_id=0"); 

我在哪里错了?

回答

1

ConnectionFactory.getConnection()在版本5后支持。黑莓8900版本是5.x还是4.x?我认为它是4.x.

你可以试试这个: HTTPConnection httpConnector = (HttpConnection) Connector.open(url);

并获得响应: httpConnector.setRequestMethod(HttpConnection.GET); InputStream in = httpConnector.openInputStream();