【发布时间】:2017-11-16 22:59:50
【问题描述】:
我想通过网络服务器接收和发送数据,但代码不起作用 我该怎么做才能让这段代码起作用?
注意 onCreate 里面的这段代码
try {
URL url = new URL("http://myweb.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream Stream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(Stream);
BufferedReader b = new BufferedReader(reader);
StringBuilder s = new StringBuilder();
String str ="";
while ((str = b.readLine())!=null) {
s.append(str);
}
String data = s.toString();
TextView myText = (TextView) findViewById(R.id.Text);
myText.setText(data);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
请
does not work详细说明。你有错误吗?没有服务器响应?收到意外响应代码?