我是android世界的新手。我试图开发一个应用程序,我尝试从未知来源下载和安装.apk。实际上我的这个.apk保存在Url地址http://103.XX.XX.38:8080/apk/HelloAndroid.apk的开发服务器上。这个网址是绝对正确的,因为当我通过Firefox查看它时会显示一个对话框来下载此.apk。现在我在我的Android应用程序中使用了这个url,其代码如下所示
try {
System.out.println("first of nethod");
URL url = new URL("http://103.XX.XX.38:8080/apk/HelloAndroid.apk");
System.out.println("second of method");
System.out.println("1");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
System.out.println("2");
urlConnection.setRequestMethod("GET");
System.out.println("3");
urlConnection.setDoOutput(true);
System.out.println("4");
urlConnection.connect();
System.out.println("5");
File sdcard = Environment.getExternalStorageDirectory();
System.out.println("6");
File file = new File(sdcard, "filename.apk");
System.out.println("7");
FileOutputStream fileOutput = new FileOutputStream(file);
System.out.println("8");
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
}
fileOutput.close();
// this.checkUnknownSourceEnability();
this.installApk();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
但我得到了例外
05-13 15:52:03.120: W/System.err(12368): java.net.ConnectException: failed to connect to /103.XX.XX.38 (port 8080): connect failed: ECONNREFUSED (Connection refused)
05-13 15:52:03.120: W/System.err(12368): at libcore.io.IoBridge.connect(IoBridge.java:114)
05-13 15:52:03.120: W/System.err(12368): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-13 15:52:03.120: W/System.err(12368): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-13 15:52:03.120: W/System.err(12368): at java.net.Socket.connect(Socket.java:842)
05-13 15:52:03.120: W/System.err(12368): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
05-13 15:52:03.120: W/System.err(12368): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-13 15:52:03.120: W/System.err(12368): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-13 15:52:03.120: W/System.err(12368): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-13 15:52:03.120: W/System.err(12368): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-13 15:52:03.130: W/System.err(12368): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-13 15:52:03.130: W/System.err(12368): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-13 15:52:03.130: W/System.err(12368): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-13 15:52:03.130: W/System.err(12368): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-13 15:52:03.130: W/System.err(12368): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
05-13 15:52:03.130: W/System.err(12368): at com.example.test.TestOne.Update(TestOne.java:136)
05-13 15:52:03.130: W/System.err(12368): at com.example.test.TestOne$1$1$1.doInBackground(TestOne.java:71)
05-13 15:52:03.130: W/System.err(12368): at com.example.test.TestOne$1$1$1.doInBackground(TestOne.java:1)
05-13 15:52:03.130: W/System.err(12368): at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-13 15:52:03.130: W/System.err(12368): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-13 15:52:03.130: W/System.err(12368): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-13 15:52:03.130: W/System.err(12368): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-13 15:52:03.140: W/System.err(12368): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-13 15:52:03.140: W/System.err(12368): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-13 15:52:03.140: W/System.err(12368): at java.lang.Thread.run(Thread.java:856)
05-13 15:52:03.140: W/System.err(12368): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
05-13 15:52:03.140: W/System.err(12368): at libcore.io.Posix.connect(Native Method)
05-13 15:52:03.140: W/System.err(12368): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-13 15:52:03.140: W/System.err(12368): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-13 15:52:03.140: W/System.err(12368): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-13 15:52:03.140: W/System.err(12368): ... 23 more
在逐步调试之后执行up-to
urlConnection.setDoOutput(true);
我在
中遇到问题 urlConnection.connect();
我正在使用Real设备Sony Tablet S(4.0.3)。任何帮助都非常感谢。在此先感谢所有
答案 0 :(得分:0)
问题解决了愚蠢的问题。这是来自真实设备的网络可访问性问题。我的网络受Cyberoam保护,因此对于真实的设备,当我使用异常消失时,我需要Cyberoam的凭证。