【问题标题】:blackberry GET request with Java Me使用 Java Me 的黑莓 GET 请求
【发布时间】:2011-06-17 13:47:16
【问题描述】:

我正在尝试获取向 php 服务发送 GET 请求的数据,但不幸的是我没有得到任何结果,我使用的是 Blackberry Simulator 9800,这是我的代码,

    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();
        }
    }

请告诉我这是什么问题,

【问题讨论】:

    标签: blackberry java-me get request


    【解决方案1】:

    你必须使用这个: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();
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多