2011-06-17 115 views
0

我想獲得的數據發送GET請求到PHP服務,但不幸的是我沒有得到任何結果,我使用BlackBerry模擬器9800,這裏是我的代碼,黑莓GET請求

HttpConnection conn = null; 
    InputStream in = null; 
    StringBuffer buff = new StringBuffer(); 
    String result = ""; 

    String url = "http://www.devbrats.com/testing/ActorRated/Service/cities.php"; 
    try{ 
     conn = (HttpConnection) Connector.open(url,Connector.READ_WRITE, true); 
     conn.setRequestMethod(HttpConnection.GET); 
     conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0"); 
     if(conn.getResponseCode() == HttpConnection.HTTP_OK){ 
      in = conn.openInputStream(); 
      //parser.parse(in, handler); 
      buff.append(IOUtilities.streamToBytes(in)); 
      result = buff.toString(); 
     } 
     else{ 
      result = "Error in connection"; 
     } 

    } catch(Exception ex){ 
     ex.printStackTrace(); 
    } finally{ 
     try { 
      if(in != null){ 
       in.close(); 
      } 
      conn.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

請告訴我什麼是它存在的問題,

回答

4

你將不得不使用這樣的:http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/io/transport/ConnectionFactory.html

要正確地創建具有正確參數的網絡連接。

如果你不使用OS5 +使用:http://www.versatilemonkey.com/HttpConnectionFactory.java

package mypackage; 

import java.io.IOException; 
import java.io.InputStream; 

import javax.microedition.io.HttpConnection; 

import net.rim.device.api.io.IOUtilities; 
import net.rim.device.api.io.transport.ConnectionFactory; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.MainScreen; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class MyScreen extends MainScreen { 

    private LabelField lbl; 

    /** 
    * Creates a new MyScreen object 
    */ 
    public MyScreen() { 
     setTitle("MyTitle"); 
     new Thread(new ConnectThread(this)).start(); 
     lbl = new LabelField(); 
     this.add(lbl); 
    } 

    public void update(Object object) { 
     synchronized (UiApplication.getEventLock()){ 
      lbl.setText((String)object); 
     } 
    } 

    private class ConnectThread implements Runnable { 

     private MainScreen screen; 

     public ConnectThread(MainScreen screen) { 
      this.screen = screen; 
     } 

     public void run() { 
      HttpConnection conn = null; 
      InputStream in = null; 
      StringBuffer buff = new StringBuffer(); 
      String result = ""; 

      String url = "http://www.devbrats.com/testing/ActorRated/Service/cities.php"; 
      try { 
       conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection(); 
       conn.setRequestMethod(HttpConnection.GET); 
       conn.setRequestProperty("User-Agent", 
         "Profile/MIDP-1.0 Confirguration/CLDC-1.0"); 

       if (conn.getResponseCode() == HttpConnection.HTTP_OK) { 
        in = conn.openInputStream(); 
        // parser.parse(in, handler); 
        buff.append(IOUtilities.streamToBytes(in)); 
        result = buff.toString(); 
       } else { 
        result = "Error in connection" + conn.getResponseCode(); 
       } 
       ((MyScreen)screen).update(result); 

      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } finally { 
       try { 
        if (in != null) { 
         in.close(); 
        } 
        conn.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
}