Android中的inputStream.getInputStream()失败

时间:2013-07-23 11:08:23

标签: android inputstream httpurlconnection

我的应用程序检查是否有更新的版本要使用。此过程在版本2中运行良好,但版本4.1无效。

private class ExecuteUpdate implements Runnable {
    @Override
    public void run() {
        try {
            updateIntent = Updater.executeUpdate(updateHandler);

            updateHandler.sendEmptyMessage(0);
        } catch (IOException e) {
            Message msg = new Message();
            msg.obj = e;

            updateHandler.sendMessage(msg);
        }
    }
}

public static Intent executeUpdate(Handler updateHandler) throws IOException {
    URL url = new URL(Application.getInstance().getURL()
            + "/Prisma3Mobile/Prisma3Mobile.apk");
    HttpURLConnection urlConnection = (HttpURLConnection) url
            .openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, "Prisma3Mobile.apk");

    FileOutputStream fileOutput = new FileOutputStream(file);
    InputStream inputStream = urlConnection.getInputStream();

    byte[] buffer = new byte[1024];
    int bufferLength = 0;
    int contentLength = urlConnection.getContentLength();

    // Determina valor máximo do ProgressDialog
    Message msg = new Message();
    msg.obj = contentLength;
    updateHandler.sendMessage(msg);

    while ((bufferLength = inputStream.read(buffer)) > 0) {

        // Atualiza ProgressDialog
        msg = new Message();
        msg.obj = file.length();            
        updateHandler.sendMessage(msg);

        fileOutput.write(buffer, 0, bufferLength); 
    }

    fileOutput.close();

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                    + "/Prisma3Mobile.apk")),
            "application/vnd.android.package-archive");

    return intent;
}

出现以下问题:

07-23 08:04:24.625: W/System.err(14510): org.apache.http.conn.HttpHostConnectException: Connection to http://"192.168.1.30" refused
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
07-23 08:04:24.655: W/System.err(14510):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 08:04:24.655: W/System.err(14510):    at com.sisteplantbrasil.util.Updater.checkForUpdates(Updater.java:74)
07-23 08:04:24.655: W/System.err(14510):    at com.sisteplantbrasil.Main$Checkcnn.doInBackground(Main.java:133)
07-23 08:04:24.655: W/System.err(14510):    at com.sisteplantbrasil.Main$Checkcnn.doInBackground(Main.java:1)
07-23 08:04:24.655: W/System.err(14510):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 08:04:24.655: W/System.err(14510):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-23 08:04:24.660: W/System.err(14510):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-23 08:04:24.665: W/System.err(14510):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 08:04:24.665: W/System.err(14510):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-23 08:04:24.665: W/System.err(14510):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-23 08:04:24.665: W/System.err(14510):    at java.lang.Thread.run(Thread.java:856)
07-23 08:04:24.665: W/System.err(14510): Caused by: java.net.ConnectException: failed to connect to /192.168.1.30 (port 80): connect failed: ENETUNREACH (Network is unreachable)
07-23 08:04:24.670: W/System.err(14510):    at libcore.io.IoBridge.connect(IoBridge.java:114)
07-23 08:04:24.670: W/System.err(14510):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-23 08:04:24.670: W/System.err(14510):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-23 08:04:24.670: W/System.err(14510):    at java.net.Socket.connect(Socket.java:842)
07-23 08:04:24.670: W/System.err(14510):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-23 08:04:24.670: W/System.err(14510):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-23 08:04:24.670: W/System.err(14510):    ... 16 more
07-23 08:04:24.670: W/System.err(14510): Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
07-23 08:04:24.675: W/System.err(14510):    at libcore.io.Posix.connect(Native Method)
07-23 08:04:24.675: W/System.err(14510):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
07-23 08:04:24.675: W/System.err(14510):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-23 08:04:24.675: W/System.err(14510):    at libcore.io.IoBridge.connect(IoBridge.java:112)

1 个答案:

答案 0 :(得分:0)

07-23 08:04:24.625: W/System.err(14510): org.apache.http.conn.HttpHostConnectException: Connection to http://"192.168.1.30" refused

07-23 08:04:24.665: W/System.err(14510): Caused by: java.net.ConnectException: failed to connect to /192.168.1.30 (port 80): connect failed: ENETUNREACH (Network is unreachable)

您确定已连接到本地网络吗?并且你允许用你的4.1手机做这个GET帖子吗?