Android上的Http Url连接问题抛出java.lang.IllegalStateException:已经连接

时间:2016-03-03 15:27:06

标签: java android multithreading android-intent android-asynctask

你好每一个我开发一些应用程序,它使用Web服务进行Http Connection,以检查服务器中的用户名和密码。 当我进行登录时总是抛出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递减低于0:无匹配的setHasTransientState调用对   03-03 17:19:27.607 19012-20724 /? E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1   03-03 17:19:27.607 19012-20724 /? E / AndroidRuntime:进程:com.mycompany.logintoapp,PID:19012   03-03 17:19:27.607 19012-20724 /? E / AndroidRuntime:java.lang.RuntimeException:执行doInBackground()时发生错误   03-03 17:19:27.607 19012-20724 /? E / AndroidRuntime:在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:在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:引起:java.lang.IllegalStateException:已经连接   03-03 17:19:27.607 19012-20724 /? E / AndroidRuntime:在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:在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:在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专注!! hideClipTray()   03-03 17:19:27.628 19012-19012 /? E / CliptrayUtils:hideClipTrayIfNeeded()TextView专注!! 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

2 个答案:

答案 0 :(得分:0)

就像一个FYI,你只需要一个带有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);

确保关闭所有流,连接等,为下次通话做好准备。

答案 1 :(得分:0)

根据this问题,此异常可能会误导并隐藏错误网址的问题。我建议您验证您的远程服务器是否按预期运行,并且您尝试连接的URL确实可以访问。