如何解决应用程序未连接到服务器urlconnection
这是我的DownloadActivity.java:
public void onClickUpdate(View view) throws IOException {
HandlerThread handlerThread=new HandlerThread("test");
handlerThread.setPriority(1);
handlerThread.start();
Looper looper=handlerThread.getLooper();
Handler handler=new Handler(looper);
handler.post(new Runnable() {
@Override
public void run() {
try {
String url="theurl";
URL u = new URL(url);
URLConnection conn = u.openConnection();
conn.addRequestProperty("version",version);
conn.setConnectTimeout(15000);
conn.connect();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
File file=new File(Environment.DIRECTORY_DOWNLOADS,"debug.apk");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(file));
fos.write(buffer);
fos.flush();
fos.close();
fos=null;
conn=null;
stream=null;
} catch(IOException e) {
Toast.makeText(SettingsActivity.this,"hit different error",Toast.LENGTH_LONG).show();
}
catch(Exception e){
e.printstacktrace();
}
}
});
}
我也尝试使用thread,ThreadPool,但这没有用,我确实在AndroidManifest.xml中添加了互联网权限
日志: 2020-09-26 09:38:11.432 27085-27085 / com.testapp.test V /字体:CurFontPath:/system/fonts/Roboto-Regular.ttf 2020-09-26 09:38:11.432 27085-27085 / com.testapp.test D / Typeface:切换至android orgin ttf
--------- beginning of system
2020-09-26 09:38:11.446 27085-27085 / com.testapp.test V /字体:CurFontPath:/system/fonts/Roboto-Regular.ttf
2020-09-26 09:38:11.446 27085-27085 / com.testapp.test D / Typeface:切换到android orgin ttf
2020-09-26 09:38:11.501 27085-27118 / com.testapp.test W / Adreno-EGL:
异常堆栈跟踪: 2020-09-27 01:13:07.175 15138-15138 / com.testapp.test V /字体:CurFontPath:/system/fonts/Roboto-Regular.ttf 2020-09-27 01:13:07.175 15138-15138 / com.testapp.test D / Typeface:切换到android orgin ttf
--------- beginning of system
2020-09-27 01:13:07.188 15138-15138 / com.testapp.test V /字体:CurFontPath:/system/fonts/Roboto-Regular.ttf
2020-09-27 01:13:07.188 15138-15138 / com.testapp.test D / Typeface:切换到android orgin ttf
2020-09-27 01:13:07.207 15138-15288 / com.testapp.test D / NetworkSecurityConfig:未指定网络安全配置,使用平台默认值
2020-09-27 01:13:07.217 15138-15288 / com.testapp.test I / DpmTcmClient:RegisterTcmMonitor from:com.android.okhttp.TcmIdleTimerMonitor
2020-09-27 01:13:07.223 15138-15288 / com.testapp.test I / DpmTcmClient:RegisterTcmMonitor from:com.android.okhttp.TcmIdleTimerMonitor
2020-09-27 01:13:07.231 15138-15173 / com.testapp.test W / Adreno-EGL:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。