2014-01-19 98 views
5

我在connection.setDoInput(true);安卓:java.lang.IllegalStateException:已經連接

HttpsURLConnection connection = null; 
DataOutputStream outputStream = null; 
DataInputStream inputStream = null; 

String urlServer = "https://www.myurl.com/upload.php"; 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 

int bytesRead = 0; 
int bytesAvailable = 0; 
int bufferSize = 0; 
byte[] buffer = null; 
int maxBufferSize = 1*1024*1024; 

try { 
    FileInputStream fileInputStream = new FileInputStream(new File(params[0])); 

    URL url = new URL(urlServer); 

    connection = (HttpsURLConnection) url.openConnection(); 
    connection.setConnectTimeout(1000); 

    // Allow Inputs & Outputs 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 

    // Enable POST method 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 

    connection.setConnectTimeout(1000); 

    outputStream = new DataOutputStream(connection.getOutputStream()); 
    outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
    outputStream.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + params[0] + "\"" + lineEnd); 
    outputStream.writeBytes(lineEnd); 

    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 

    // Read file 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

    while (bytesRead > 0) { 
     outputStream.write(buffer, 0, bufferSize); 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    } 

    outputStream.writeBytes(lineEnd); 
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

測試的代碼樣本,但它總是錯誤的錯誤日誌是java.lang.IllegalStateException: Already connected。 我嘗試了這些,但沒有工作:

connection.setRequestProperty("Connection", "close"); 
connection.disconnect(); 
connection.setConnectTimeout(1000); 

編輯:即使我沒有打電話connection.connect(),它仍然給了同樣的錯誤already connected

+0

在使用它們後關閉了'OutputStreamWriter'和'InputStreamReader'嗎? – alfasin

+0

是的。並且在使用OutputStreamWriter和InputStreamReader的行之前發生錯誤。 –

+0

@downvoter:爲什麼降低投票率?我已經閱讀並嘗試了其他類似的問題,但都沒有工作,這就是爲什麼我發佈這個。 –

回答

0

移動

connection.connect(); 

connection.setRequestMethod("POST"); 
+2

Brian已發佈了此答案16分鐘前... – alfasin

0

有關於HTTP連接here

一個非常好的職位的底線是針對自己只需要滿足以下條件。

HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection(); 
// setDoOutput(true) implicitly set's the request type to POST 
connection.setDoOutput(true); 

我不知道您需要可以指定HttpsURLConnection的。您可以使用HttpURLConnection連接到Https網站。讓java爲你幕後工作。

這裏是我使用JSON帖子

public static String doPostSync(final String urlToRead, final String content) throws IOException { 
    final String charset = "UTF-8"; 
    // Create the connection 
    HttpURLConnection connection = (HttpURLConnection) new URL(urlToRead).openConnection(); 
    // setDoOutput(true) implicitly set's the request type to POST 
    connection.setDoOutput(true); 
    connection.setRequestProperty("Accept-Charset", charset); 
    connection.setRequestProperty("Content-type", "application/json"); 

    // Write to the connection 
    OutputStream output = connection.getOutputStream(); 
    output.write(content.getBytes(charset)); 
    output.close(); 

    // Check the error stream first, if this is null then there have been no issues with the request 
    InputStream inputStream = connection.getErrorStream(); 
    if (inputStream == null) 
     inputStream = connection.getInputStream(); 

    // Read everything from our stream 
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(inputStream, charset)); 

    String inputLine; 
    StringBuffer response = new StringBuffer(); 

    while ((inputLine = responseReader.readLine()) != null) { 
     response.append(inputLine); 
    } 
    responseReader.close(); 

    return response.toString(); 
} 
1
  1. 閱讀它結束流後,必須關閉輸入流偵錯碼。

  2. 您應該刪除呼叫connect()。你把它放在錯誤的地方,但它是自動的,根本不需要被調用。

  3. 您還可以刪除設置POST的行。這在調用setDoOutput(true)時是隱含的。

  4. 您還可以刪除複製循環中的大部分垃圾。使用固定大小的緩衝區:

    while ((count = in.read(buffer)) > 0) 
    { 
        out.write(buffer, 0, count); 
    } 
    

    不要在每次讀取時使用新的緩衝區;不要撥打available();不要通過GO;不要收200美元。

-1

添加

if (connection != null) connection.disconnect(); 

connection = (HttpsURLConnection) url.openConnection(); 

看看問題解決了。如果是,則表示connection.disconnect()未在您的原始代碼中調用。也許你把connection.disconnect()放在你的try塊的末尾,但是在它之前發生了一個異常,所以它跳轉到catch塊並且從不調用connection.disconnect()