我是新的android开发人员。HttpUrlConnection android
我想从我的网络服务器访问数据的代码....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
URL url;
try {
url = new URL("http://www.mysite.net/LEDstate.txt");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
//System.out.print(current);
TextView t = (TextView) findViewById(R.id.ledstate);
t.setText(current);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
但数据不会出现在Screen.There是在TextView中没有变化。
我已检查权限,他们没事。
任何帮助? 谢谢。
是否有任何错误之外?如果是这样,发布logcat输出。并尝试调试。 char current有什么价值? (检查它不为空) –
没有错误....但没有显示我的数据...显示默认的Hello World! – antkan