从模拟器打开一个简单的 HttpConnection 时遇到问题,我已将 deviceside=true 后缀附加到我的 url,但它仍然无法正常工作,我收到一个响应代码为 0 的空 httpconnection。这是给我带来问题的代码:
public void readUrl(){
HttpConnection conn=null;
try {
conn = (HttpConnection) Connector.open("http://www.google.com;deviceside=true");
conn.setRequestMethod("GET");
if(conn.getResponseCode()==HttpConnection.HTTP_OK){
System.out.println("Create connection sucessfully");
}
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
DataInputStream din=null;
ByteVector responseBytes=null;
try {
din = conn.openDataInputStream();
responseBytes = new ByteVector();
int i = din.read();
while (-1 != i) {
responseBytes.addElement((byte) i);
i = din.read();
}
} catch (IOException e) {
//TODO: HANDLE EXCEPTIONS
e.printStackTrace();
}
responseBytes.toArray();
我不知道是怎么回事。它假设通过附加 deviceside=true 它应该直接连接。无论如何,我也尝试安装 MDS 服务器并将我的 url 设置为 deviceside=false,但结果是一样的。
现在我使用http://localhost:8080/resources/mypage.html 之类的本地 url 测试了相同的代码,它按预期工作,所以我想知道这是否可能是模拟器配置问题。我该如何解决?
非常感谢。