安卓开发连接tomcat服务器,程序卡在URL url = new URL处执行不下去
ImageButton download = (ImageButton) getActivity().findViewById(
R.id.download);
download.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "ssss", Toast.LENGTH_SHORT)
.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Http链接
HttpURLConnection connection = null;
try {
Toast.makeText(getActivity(), "----",
Toast.LENGTH_SHORT).show();
// 打开链接
URL url = new URL("http://125.106.5.171:8080"
+ "/huoche" + "/index.jsp");
connection = (HttpURLConnection) url
.openConnection();
// 设置属性
connection.setConnectTimeout(6000);// 设置超时时间
connection.setRequestMethod("GET");
// Post 1)容量没有限制 2) 安全
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
// 读取数据
// 1)获取位流
InputStream in = connection.getInputStream();
// 二进制-->BufferedReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
// 2) 读取
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
// 发送消息
Message msg = new Message();
msg.what = 1;
msg.obj = response.toString();
_handler.sendMessage(msg);
// Handler
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect(); // 断开链接
}
}
在真机测试中,手机和笔记本都是连接着家里的wifi,在手机创建数据库成功后,点击按钮,准备把数据库中的数据写入,发现程序一直停在URL url = new URL处,无法连接。请问各位大神,应该怎么办呢?