1

你好我每個人都開發了一些應用程序,使Web服務Http連接檢查服務器中的用戶名和密碼。 當我做登錄,它總是拋出java.lang.IllegalStateException:已經連接這個例外,它是從今天開始,yestrday它perfetcly工作 這是我的代碼來連接:問題與Http Url連接上android throw 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:致命例外: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:at android.os.AsyncTask $ 3.done(AsyncTask.java:300) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在java.util.concurrent.FutureTask.setException(FutureTask.java:222) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在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:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在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:在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:在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:在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 03-03 17:19:27.607 19012-20724 /? E/AndroidRuntime:在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()

我沒有明白是什麼問題yestdrday它的工作

編輯:例外總是扔在這行

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

+0

我在用PHP編寫的REST API服務器上遇到了類似的問題,但只有LG G3和5.0也是如此......如果我能弄清楚發生了什麼,我一定會回來寫一個更新。 –

回答

0

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

並確保關閉所有的流,連接等,準備下次調用。

0

根據this的問題,這個例外可能會引起誤解,並隱藏了一個錯誤的URL的問題。我建議您驗證您的遠程服務器是否按預期運行,並且您嘗試連接的URL確實可以訪問。