2013-03-13 50 views
10

下面是我的代码:如何停止HttpURLConnection.getInputStream()?

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

当我上传的文件,该行is = connection.getInputStream();会花很多的时间去回复。所以我想实现一个停止功能,如stopupload()。但是如果我在代码在is = connection.getInputStream();处处理代码时调用stopupload(),它仍然需要等待它的回复。

我想在执行stopupload()时立即停止等待。我该怎么做?

+1

Thread.interrupt()(编辑:实际上,这可能不起作用,因为此API不可中断) – njzk2

+0

是否有任何方法没有使用线程? – brian

+0

做'connection = null; is = null;'实际上停止了AsyncTask的执行?我在问,因为'connection = null;'不适用于我,我怀疑is = null;'只会在getInputStream结束时才分配is。你最终得到了你想要的东西吗? – doplumi

回答

3

但如果我叫stopupload()而代码在行is = connection.getInputStream();处理,它仍然需要等待答复。

开始HoneyComb,所有的网络操作都不允许通过主线程执行。为避免获得NetworkOnMainThreadException,您可以使用ThreadAsyncTask

我想在执行stopupload()时立即停止等待。我如何 做到这一点?

下面的代码让用户在2秒后停止上传,但您可以相应地修改睡眠时间(应小于5秒)。

上传

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... 
} 
+18

地狱是如何被接受的解决方案? stopUpload()未实现。 – AsafK

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) { 

     }  
    } 
    } 
0

裹使用HttpURLConnection一个Future内,如所描述here的代码。