android在网络编程的时候没有执行connection.getResponseCode()!=200
1.用真机测试的时候,电脑必须和手机连同一个局域网。
2.必须开新线程,不放在主线程里面访问。
new Thread(){
public void run() {
Looper.prepare();
try {
URL url=new URL("http://192.168.191.1:8080/movie/servlet/FileServlet");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode()!=200) {
Log.e("aaa", url.toString());
throw new Exception("URL请求失败!");
}
Log.e("bbb", url.toString());
InputStream inputStream=connection.getInputStream();
result=readData(inputStream, "UTF-8");
} catch (Exception e) {
// TODO: handle exception
}
myhandle.sendEmptyMessage(0x1122);
};
}.start();