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

多くの電話や一部のシミュレーターでは、別のスレッドでしか接続を開くことができません。つまり、メインの therad = UI スレッドに接続しようとすると、機能せず、何も通知されません。メソッドを新しいスレッドに単純にラップしてみてください

于 2013-03-01T16:00:53.473 に答える