[英]HttpURLConnection works on emulator but not on the device
I'm trying to experiment the HttpURLConnection to get some XML from a server. 我正在尝试HttpURLConnection来从服务器获取一些XML。 This is the code I'm using: 这是我正在使用的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myConn = this.getString(R.string.myConnection);
HttpURLConnection httpConnection = null;
InputStream in = null;
try {
URL mySite = new URL(myConn);
URLConnection connection = mySite.openConnection();
TextView tv = (TextView)this.findViewById(R.id.myTextView);
httpConnection = (HttpURLConnection)connection;
in = httpConnection.getInputStream();
String myString = convertStreamToString(in);
tv.setText(myString);
} catch (IOException ex) {} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
httpConnection.disconnect();
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
On the emulator this code works and i can see the stream on the TextView... 在模拟器上,此代码有效,我可以在TextView上看到流。
On my device I can't see anything (3g connection).. 在我的设备上,我什么都看不到(3g连接)。
Did I miss something? 我错过了什么?
thanks 谢谢
通过使用AsyncTask您可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.