gpt4 book ai didi

java - Android 上的 Http Url 连接问题抛出 java.lang.IllegalStateException : Already connected

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:39 26 4
gpt4 key购买 nike

大家好,我正在开发一些应用程序,它与 Web 服务建立 Http 连接,以检查服务器中的用户名和密码。当我登录时总是抛出 java.lang.IllegalStateException: 已连接此异常并且从今天开始,昨天它工作得很好这是我的连接代码:

String username = params[0];
String password = params[1];
String res="";
HttpURLConnection urlConnection=null;
String urllogin=getResources().getString(R.string.AppLogin);
Content="phone_no="+URLEncoder.encode(username)+"&pass="+URLEncoder.encode(password);




try{
URL url=new URL(urllogin);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Connection","close");
urlConnection.setRequestProperty("Content-Length", String.valueOf(Content.getBytes().length));
urlConnection.setRequestMethod("POST");
urlConnection.setChunkedStreamingMode(0);
OutputStream outputStream=new BufferedOutputStream(urlConnection.getOutputStream());
outputStream.write(Content.getBytes());

if(urlConnection.getResponseCode()==HttpURLConnection.HTTP_OK)
{
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
res = readStream(in);
}
if(urlConnection.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
InputStream in = new BufferedInputStream(urlConnection.getErrorStream());
res = readStream(in);
}

这是抛出异常时的日志或错误:

03-03 17:18:52.831 19012-19012/? E/View: hasTransientState decremented below 0: unmatched pair of setHasTransientState calls 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: Process: com.mycompany.logintoapp, PID: 19012 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground() 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:300) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Already connected 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.net.URLConnection.checkNotConnected(URLConnection.java:463) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.net.URLConnection.setDoOutput(URLConnection.java:877) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at com.mycompany.logintoapp.MainActivity$ValidateLogin.doInBackground(MainActivity.java:275) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at com.mycompany.logintoapp.MainActivity$ValidateLogin.doInBackground(MainActivity.java:225) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:288) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)  03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)  03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)  03-03 17:19:27.607 19012-20724/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)  03-03 17:19:27.619 19012-19012/? E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray() 03-03 17:19:27.628 19012-19012/? E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()

我不明白昨天的问题是什么

编辑:异常总是在此行中抛出

urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Connection","close");
urlConnection.setRequestProperty("Content-Length", String.valueOf(Content.getBytes().length));

EDIT2:现在我发帖时总是缺少参数,但奇怪的是它只在 LG g3 5.0 中

最佳答案

仅供引用,您只需要一个带有 else 的 if 语句。只需在每个子句中指定您需要哪个 BufferedInputStream 即可。然后再进行设置即可。这样您只需检查一次响应代码。不确定这是否是您的问题,但它肯定会对您有所帮助,以确保您只做一次事情。使问题的调试变得更加容易。

InputStream inputStream;
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = urlConnection.getInputStream();
} else {
inputStream = urlConnection.getErrorStream();
}

InputStream in = new BufferedInputStream(inputStream);
res = readStream(in);

并确保关闭所有流、连接等,为下一次调用做好准备。

关于java - Android 上的 Http Url 连接问题抛出 java.lang.IllegalStateException : Already connected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com