ブラックベリープラットフォームでhttpリクエストを作成しようとしています。問題は次のとおりです。WiFi経由で接続すると正常に機能しますが、モバイルネットワークを使用すると機能せず、接続がフリーズします。
これが私のコードです
public static String getHttpUTFResponse(String url) {
url = addConnSuffex(url);
HttpConnection connection = null;
byte responseData[] = null;
try {
connection = (HttpConnection) new ConnectionFactory()
.getConnection(url).getConnection();
int len = (int) connection.getLength();
System.out.println(len);
if (len != -1) {
responseData = new byte[len];
DataInputStream dis;
dis = new DataInputStream(connection.openInputStream());
dis.readFully(responseData);
dis.close();
}
} catch (IOException e) {
System.out.println("Connection Error");
} finally {
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = null;
}
}
if (responseData != null) {
try {
return new String(responseData, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
public static String addConnSuffex(String url) {
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] records = sb.findRecordsByCid("WPTCP");
String uid = null;
if (records == null) {
return url;
}
for (int i = 0; i < records.length; i++) {
if (records[i].isValid() && !records[i].isDisabled()) {
if (records[i].getUid() != null
&& records[i].getUid().length() != 0) {
if ((records[i].getUid().toLowerCase().indexOf("wifi") == -1)
&& (records[i].getUid().toLowerCase()
.indexOf("mms") == -1)) {
uid = records[i].getUid();
break;
}
}
}
}
if (DeviceInfo.isSimulator()) {
return url;
}
if (uid != null) {
// open a WAP 2 connection
url = url + ";deviceside=true;ConnectionUID=" + uid;
System.out.println("**************** on conn" + url);
} else {
url = url + ";deviceside=true;interface=wifi";
// Consider another transport or alternative action.
}
System.out.println("**************** on conn" + url);
return url;
}
どうもありがとう