10

Below is my code:

private HttpURLConnection connection;
private InputStream is;

public void upload() {
    try {
        URL url = new URL(URLPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.connect();
        is = connection.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void stopupload() {
    connection = null;
    is = null;
}

When I upload file, the line is = connection.getInputStream(); will spend a lot of time to get reply. So I want to implement a stop function as stopupload(). But if I call stopupload() while the code is handling at line is = connection.getInputStream();, it still needs to wait for its reply.

I want to stop waiting at once while implement stopupload(). How can I do it?

brian
  • 6,802
  • 29
  • 83
  • 124
  • 1
    Thread.interrupt() (edit : actually, that probably doesn't work as this API is not interruptible) – njzk2 Mar 13 '13 at 09:11
  • Whether any methods without use thread? – brian Mar 13 '13 at 09:14
  • does `connection = null; is = null;` actually stops the execution of the AsyncTask? I am asking because `connection = null;` doesn't work for me and I doubt `is = null;` will work given that the is is assigned only at the end of getInputStream. Do you actually obtained what you wanted, in the end? – doplumi Feb 03 '14 at 16:46

3 Answers3

2

But if I call stopupload() while the code is handling at line is = connection.getInputStream();, it still needs to wait for its reply.

Starting from HoneyComb, all network operations are not allowed to be executed over main thread. To avoid getting NetworkOnMainThreadException, you may use Thread or AsyncTask.

I want to stop waiting at once while implement stopupload(). How can I do it?

Below code gives the user to stop uploading after 2 seconds, but you can modify the sleep times (should be less than 5 seconds) accordingly.

upload:

public void upload() {
    try {
        URL url = new URL(URLPath);
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(30000);
        connection.setReadTimeout(30000);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.connect();
        // run uploading activity within a Thread
        Thread t = new Thread() {
            public void run() {
                is = connection.getInputStream();
                if (is == null) {
                    throw new RuntimeException("stream is null");
                }
                // sleep 2 seconds before "stop uploading" button appears
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        mBtnStop.setVisibility(View.VISIBLE);
                    }
                }, 2000);
            }
        };
        t.start();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
}

onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    // more codes...
    Handler mHandler = new Handler();
    mBtnStop = (Button) findViewById(R.id.btn_stop);
    mBtnStop.setBackgroundResource(R.drawable.stop_upload);
    mBtnStop.setOnClickListener(mHandlerStop);
    mBtnStop.setVisibility(View.INVISIBLE);

    View.OnClickListener mHandlerStop = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopUpload(); // called when "stop upload" button is clicked
        }
    };

    // more codes...
}
melvynkim
  • 1,655
  • 3
  • 25
  • 38
2
private HttpURLConnection connection;
private InputStream is;

   public void upload() {
       try {
    URL url = new URL(URLPath);
    connection = (HttpURLConnection) url.openConnection();
    connection.setConnectTimeout(30000);
    connection.setReadTimeout(30000);
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.connect();

    Thread t = new Thread() {
        public void run() {
             is = connection.getInputStream();                
        }
    };
    t.start()
} catch (Exception e) {
    e.printStackTrace();
}catch (InterruptedException e) {
        stopupload(connection ,is, t);
    }      
}

public void stopupload(HttpURLConnection connection ,InputStream  is,Thread t) {
  if(connection != null ){
    try {
           t.interupt();
               running = false;
               connection=null;
           is=null;
        } catch (Exception e) {

        }      
     }   
  }
Fred Ondieki
  • 2,314
  • 25
  • 23
0

Wrap the code that uses HttpURLConnection inside a Future, as described here.

Community
  • 1
  • 1
Edward Brey
  • 40,302
  • 20
  • 199
  • 253