简体   繁体   中英

Failed inputStream.getInputStream() in Android

My application checks if there is a newer version to be used. This process works well in version 2, but version 4.1 is not working.

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;
}

The following problem occurs:

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)

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)

Are you sure that you are connected to your local network ? And that you are allow to do this GET post with your 4.1 phone ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM