2012-12-02 25 views
0

我對android編程非常陌生,現在我一直堅持使用這段代碼。 我得到的錯誤是「無效的SingleClientConnManager使用:連接仍然分配。」 - 我明白爲什麼會發生這種情況,但不知道如何解決。使用SingleClientConnManager和AsyncTask時出錯

protected class loader extends AsyncTask<String, Void, String> { 

    protected void onPreExecute() 
    { 
     nameValuePairs = new ArrayList<NameValuePair>(1); 
     nameValuePairs.add(new BasicNameValuePair("username_in",username.getText().toString().trim())); 
     nameValuePairs.add(new BasicNameValuePair("password_in",password.getText().toString().trim())); 
     tv.setText("Started onPreExecute"); 

    } 

    @Override 
    protected String doInBackground(String... params) { 

     try { 
      httpclient = new DefaultHttpClient(); 
      httppost = new HttpPost("http://www.myurl.com/phplogin.php"); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      response=httpclient.execute(httppost); 

      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      final String response = httpclient.execute(httppost, responseHandler); 

      //Toast.makeText(MainActivity.this,response, Toast.LENGTH_LONG).show(); 
      if(response.equalsIgnoreCase("User Found")) 
      { 
       httppost.getEntity().consumeContent(); 
       //startActivity(new Intent("com.example.obligatorisk.BROWSER")); 
      } 
     } 
     catch(IOException e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String result) 
    { 
     tv.setText("Reached onPostExecute"); 
    } 
    // TODO Auto-generated method stub  
} 

完整的錯誤日誌:我在工作中的代碼

10-18 14:30:52.134: W/SingleClientConnManager(3940): Invalid use of SingleClientConnManager: connection still allocated. 
10-18 14:30:52.134: W/SingleClientConnManager(3940): Make sure to release the connection before allocating another one. 
10-18 14:30:52.414: W/System.err(3940): org.apache.http.client.HttpResponseException: Internal Server Error 
10-18 14:30:52.414: W/System.err(3940):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71) 
10-18 14:30:52.424: W/System.err(3940):  at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59) 
10-18 14:30:52.424: W/System.err(3940):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) 
10-18 14:30:52.424: W/System.err(3940):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) 
10-18 14:30:52.424: W/System.err(3940):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) 
10-18 14:30:52.424: W/System.err(3940):  at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:113) 
10-18 14:30:52.424: W/System.err(3940):  at com.example.obligatorisk.MainActivity$loader.doInBackground(MainActivity.java:1) 
10-18 14:30:52.424: W/System.err(3940):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
10-18 14:30:52.434: W/System.err(3940):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
10-18 14:30:52.434: W/System.err(3940):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
10-18 14:30:52.434: W/System.err(3940):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
10-18 14:30:52.443: W/System.err(3940):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
10-18 14:30:52.443: W/System.err(3940):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
10-18 14:30:52.443: W/System.err(3940):  at java.lang.Thread.run(Thread.java:856) 

該項目背後的想法是做一個登錄功能的應用程序。

我一直試圖在網上找到解決方案和幫助,但無論我嘗試什麼,我都無法弄清楚這一點。我真的很希望有人會花時間在這裏幫助我。如果您需要更多關於代碼,錯誤等的信息,請告訴我,我會添加它。

謝謝,並感謝所有早些時候幫助我的人。

回答

0

如果響應等於「找到用戶」,則您只在執行httppost.getEntity().consumeContent();。無論如何,你必須完全消費內容。在條件之外移動該方法調用。

ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
    final String response = httpclient.execute(httppost, responseHandler); 

    //Toast.makeText(MainActivity.this,response, Toast.LENGTH_LONG).show(); 
    if(response.equalsIgnoreCase("User Found")) 
    { 
     //startActivity(new Intent("com.example.obligatorisk.BROWSER")); 
    } 
    httppost.getEntity().consumeContent(); 
+0

我試圖這樣做,但我仍然得到相同的錯誤。 ResponseHandler responseHandler = new BasicResponseHandler(); final String response = httpclient.execute(httppost,responseHandler); //Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();如果(response.equalsIgnoreCase(「User Found」)) { startActivity(new Intent(「com.example.obligatorisk.BROWSER」)); } httppost.getEntity()。consumeContent(); – Xantrax

相關問題