3

我是安卓世界的新手。我试图开发一个应用程序,我尝试从未知来源下载和安装 .apk。实际上,我的这个 .apk 保存在 Url 地址http://103.XX.XX.38:8080/apk/HelloAndroid.apk的开发服务器上。这个 url 是绝对正确的,因为当我通过 Firefox 检查它时,它会显示一个对话框来下载这个 .apk。现在我在我的 android App 中使用了这个 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

调试后逐步执行它

      urlConnection.setDoOutput(true);

我遇到了问题

      urlConnection.connect();

我正在使用 Real device Sony Tablet S (4.0.3) 。非常感谢任何帮助。在此先感谢大家

4

1 回答 1

0

问题解决了愚蠢的问题。这是真实设备的网络可访问性问题。我的网络受 Cyber​​oam 保护,因此对于真实设备,当我使用异常消失时,我需要 Cyber​​oam 的凭据。

于 2014-05-13T13:21:38.973 回答