ここでは、そのようなことを行う方法の例を示します。
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) {
}
}
}
注:この特定のコードはテストしませんでした。以前の私のプロジェクトから嘘をついたコードを編集したので、いくつかのエラーを修正する必要があります。
ちょっとMr.Lou!あなたの答えに感謝します。私はjavaMEプロジェクトでこのメソッドを使用しています。それはEclipseで正常に動作しますが、Javaモデムデバイスで実行しようとすると、30分間動作してしまいます。 "if(connection.getResponseCode()== HttpConnection.HTTP_OK)" コードの改善?私は例外を印刷しようとしましたが、コードがフリーズし、エラーは表示されません。 – kohhworlwide