gpt4 book ai didi

java - 使用移动网络时http连接卡住

转载 作者:行者123 更新时间:2023-12-01 15:26:12 25 4
gpt4 key购买 nike

我正在尝试在黑莓平台上发出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;
}

非常感谢

最佳答案

对于网络连接 TransportDetective.class 更有用,可以解决很多与网络相关的问题。

TransportDetective y URLFactory para configurar la conexion a web

Different ways to make an HTTP or socket connection

关于java - 使用移动网络时http连接卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com