Android HttpURLConnection: java.lang.IllegalStateException: 已经连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23738940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpURLConnection: java.lang.IllegalStateException: Already connected
提问by jul
I'm trying to use HttpURLClient to send some POST data to a server using the HttpRestClientclass shown below. When executing
我正在尝试使用 HttpURLClient 使用HttpRestClient下面显示的类将一些 POST 数据发送到服务器。执行时
conn.setDoInput(true);
I get
我得到
java.lang.IllegalStateException: Already connected
I uninstalled the app, and still get the same error.
我卸载了该应用程序,但仍然出现相同的错误。
In all the example I've seen openConnection is called before setDoInput. If, as its name suggests, openConnectionopens a connection, it should never be used before `setDoInput, right? What am I missing?
在我见过的所有示例中, openConnection 之前都被调用过setDoInput。如果,顾名思义,openConnection打开一个连接,它不应该在 `setDoInput 之前使用,对吗?我错过了什么?
Maybe at some point it crashed before executing disconnect. Could that be the reason? If so, how can I disconnect the old connection?
也许在某个时候它在执行之前崩溃了disconnect。这可能是原因吗?如果是这样,我如何断开旧连接?
public class HttpRestClient {
static public int post(String urlStr, List<NameValuePair> data){
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(data));
writer.flush();
writer.close();
os.close();
InputStream is = conn.getInputStream();
String dude = readIt(is);
return 1;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
finally {
if(conn!=null) conn.disconnect();
}
}
}
回答by Sharcoux
This might be due to watches while debugging in your IDE. See this answer. It happened to me and was hard to discover.
这可能是由于在 IDE 中调试时进行了监视。看到这个答案。它发生在我身上,很难发现。
回答by Anggrayudi H
You called both of conn.setDoInput(true);and conn.setDoOutput(true);. Use one of them:
你调用了conn.setDoInput(true);和conn.setDoOutput(true);。使用其中之一:
setDoOutput(true)is used forPOSTandPUTrequests.setDoInput(true)is used forGETrequest.
setDoOutput(true)用于POST和PUT请求。setDoInput(true)用于GET请求。
The connection you made was confused, it can't decide which request should be used.
您建立的连接很混乱,它无法决定应该使用哪个请求。
In your code:
在您的代码中:
static public int post(String urlStr, List<NameValuePair> data){
HttpURLConnection conn = null;
System.setProperty("http.keepAlive", "false"); // must be set
try {
...
conn.setDoOutput(true);
conn.setRequestMethod("POST");
// and connect to server, if needed
conn.connect();
...
}
....
回答by roneo
It may be a misleading exception. See this defect for Jersey-2 https://java.net/jira/browse/JERSEY-2729
这可能是一个误导性的例外。请参阅 Jersey-2 https://java.net/jira/browse/JERSEY-2729 的此缺陷
The link has been updated: https://github.com/jersey/jersey/issues/3001
链接已更新:https: //github.com/jersey/jersey/issues/3001
Basically the issue is jersey was throwing invalid exception. The real issue in my case was that the connection was refused from the server.
基本上问题是球衣抛出了无效的异常。就我而言,真正的问题是服务器拒绝了连接。

