JavaME - HTTP 获取 - 将值发送到网站

JavaME - HTTP Get - Send Values to Website

我正在构建一个项目,我想要一个方法来发出简单的 http GET 请求,以便通过 URL 将两个变量发送到网站。 在正常的 java 项目中,我可能会使用 java.net 或 apache 并在几分钟内解决问题。在JavaME中,由于我缺乏经验,我无法真正完成任务。

基本上我想做的是 url 像 google.com/index.php?v1=x&v=y 能够执行获取请求以通过 URL.

发送这些变量

有什么建议吗?

下面是一个示例,说明如何执行类似的操作。

HttpConnection connection = null;
InputStream inputstream = null;
String url = null;
StringBuffer dataReceived = null;

    url = "http://www.google.com/index.php?v1=x&v=y";
    dataReceived = new StringBuffer();

    try {
        connection = (HttpConnection) Connector.open(url);
        connection.setRequestMethod(HttpConnection.GET);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Connection", "close");

        if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
            inputstream = connection.openInputStream();
            int ch;
            while ((ch = inputstream.read()) != -1 ) {
                dataReceived.append((char) ch);

            }
        } else {
            // Connection not ok
        }
    } catch (Exception e) {
      // Something went wrong
    } finally {
        if (inputstream != null) {
            try {
                inputstream.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

注意:我没有测试这个特定的代码。我刚刚编辑了我之前项目中的一些代码,因此您可能需要修复一些错误。