2014-01-19 150 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()