Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop HttpURLConnection.getInputStream()?

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?

like image 900
brian Avatar asked Mar 13 '13 09:03

brian


People also ask

Are there any real world examples of httpurlconnection in Java?

These are the top rated real world Java examples of java.net.HttpURLConnection.getInputStream extracted from open source projects. You can rate examples to help us improve the quality of examples.

How do I set the HTTP request header in httpurlconnection?

Call openConnection () method on URL object that returns instance of HttpURLConnection Set the request method in HttpURLConnection instance, default value is GET. Call setRequestProperty () method on HttpURLConnection instance to set request header values, such as “User-Agent” and “Accept-Language” etc.

How to send HTTP request programmatically in Java?

HttpURLConnection class from java.net package can be used to send Java HTTP Request programmatically. Today we will learn how to use HttpURLConnection in java program to send GET and POST requests and then print the response.


1 Answers

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...
}
like image 75
melvynkim Avatar answered Oct 23 '22 03:10

melvynkim