I am new in android world . I trying to developer a app where i try to download and install .apk from unknown sources. Actually my this .apk is kept on development server of Url address http://103.XX.XX.38:8080/apk/HelloAndroid.apk. This url is absolutely correct because when i check it through Firefox it showing a dialog to download this .apk. Now i used this url in my android App whose code is as below
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();
}
But i am getting a exception
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
After debug step-wise-step it executing up-to
urlConnection.setDoOutput(true);
and i ma getting problem in
urlConnection.connect();
I am using Real device Sony Tablet S (4.0.3) . Any help is really appreciated. Thanks in advance to all
Issue solved silly problem. It was issue of network accessibility from real device. My network was Cyberoam protected , so for real device i need credential for Cyberoam when i used exception gone.