0
    HttpConnection c = null;
    InputStream is = null;
    StringBuffer sb = new StringBuffer();
    String request = serverUrl; // + "?loc=" + location.getLat() + "," + location.getLng() + "data=" + message.getString();
    try {
        c = (HttpConnection) Connector.open("http://www.google.com");
        c.setRequestMethod(HttpConnection.GET); //default
        is = c.openInputStream(); // transition to connected!
        int ch = 0;
        for (int ccnt = 0; ccnt < 150; ccnt++) { // get the title.
            ch = is.read();
            if (ch == -1) {
                break;
            }
            sb.append((char) ch);
            return sb.toString();
        }
    } catch (IOException x) {
        x.printStackTrace();
    } finally {
        try {
            is.close();
            c.close();
        } catch (IOException x) {
            x.printStackTrace();
        }
    }
    System.out.println(sb.toString());
    return null;

在上面的代码中——这一行

(HttpConnection) Connector.open("http://www.google.com");

不响应。我能做些什么来解决这个问题?

4

2 回答 2

0


尝试以下

 HttpConnection c = null;
    InputStream is = null;
    StringBuffer sb = new StringBuffer();
    String request = serverUrl; // + "?loc=" + location.getLat() + "," + location.getLng() + "data=" + message.getString();
    try {
        c = (HttpConnection) Connector.open("http://www.google.com");
        c.setRequestMethod(HttpConnection.GET); //default
        int status_code=c.getResponseCode();
        if(status_code!=HttpConnection.HTTP_OK)
        {
          //Show the  failure message to user
          return;
        }

        //Remaining part is same as your code
于 2013-03-01T13:24:26.437 回答
0

在许多手机和一些模拟器中,您只能在另一个线程中打开连接。也就是说,如果您尝试在 main therad = UI 线程中连接,它将无法工作并且不会告诉您任何内容。尝试简单地将您的方法包装在一个新线程中

于 2013-03-01T16:00:53.473 回答