黑莓GET请求Java Me

时间:2011-06-17 13:47:16

标签: blackberry java-me get request

我正在尝试将数据发送到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();
        }
    }

请告诉我它有什么问题,

1 个答案:

答案 0 :(得分: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();
                }
            }
        }
    }
}