我在写一个Android项目,其中有连接服务器检查版本更新的功能,但是总是提示服务器连接异常。
部分代码如下:
/**
* 连接服务器,检查版本更新
* @author Administrator
*
*/
private class CheckVersionTask extends Thread{
@Override
public void run() {
Message msg = Message.obtain();
// TODO Auto-generated method stub
try {
URL url = new URL(getResources().getString(R.string.path));
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
System.out.println("this is right here");
conn.setRequestMethod("GET");
System.out.println("this is right here too");
//在执行到下面这一句时发生IO异常,跳转到相应的catch语句
int code = conn.getResponseCode();
System.out.println("something wrong");
if(code == 200){
InputStream is = conn.getInputStream();
updateInfo = UpdateInfoParser.getUpdateInfo(is);
if(updateInfo != null){
msg.what = PARSE_SUCCESS;
}else{
msg.what = PARSE_ERROR;
}
}else{
//TODO:给用户界面的提示
msg.what = SERVER_ERROR;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
msg.what = URL_ERROR;
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
msg.what = URL_ERROR;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("something wrong here");
e.printStackTrace();
msg.what = IO_ERROR;
}finally{
handler.sendMessage(msg);
}
}
}
在logcat中的部分打印结果如下:
各位大神有木有解决方法?
|
|