2014-02-15 46 views
2

我想知道爲什麼這個代碼不執行?我試圖通過POST方法從我的設備發送數據,但沒有錯誤。該應用程序剛剛完成自身設備上的通信「我的應用程序已停止:無效使用SingleClientConnManager的:連接仍然分配

下面是執行:

KlientNameValue kn = new KlientNameValue(getApplicationContext()); 
kn.new MyAsyncTask().execute(zam.klient.getNazwa(),zam.klient.getNip(),zam.klient.getAdres()); 

這裏是代碼:

public class KlientNameValue { 

List<NameValuePair> KlientNameValuePairs = new ArrayList<NameValuePair>(); 
Context context; 
public KlientNameValue(Context context) { 
    super(); 
    this.context=context; 
} 



public class MyAsyncTask extends AsyncTask<String, Void, Void> { 

     @Override protected Void doInBackground(String... params) { 
      // TODO  Auto-generated method stub 
      postData(params[0], params[1], params[2]); 
     return null; 

     } 

     @Override 
     protected void onPostExecute(Void result) { 

     Toast.makeText(context , "Zlecenie zostało wysłane", 
    Toast.LENGTH_LONG).show(); 
      } 

     void postData(String nazwa, String nip, String adres) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("here is my default link :)"); 

     try { // Add your data 

     KlientNameValuePairs = new ArrayList<NameValuePair>(); 
     KlientNameValuePairs.add(new BasicNameValuePair("Kli_imie", nazwa)); 
     KlientNameValuePairs.add(new BasicNameValuePair("Kli_adres", adres)); 
     KlientNameValuePairs.add(new BasicNameValuePair("Kli_nr_telefonu", 
     nip)); 



     httppost.setEntity(new UrlEncodedFormEntity(KlientNameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     //httppost.setEntity(new UrlEncodedFormEntity( 
     //  ZamowienieNameValuePairs)); // HttpResponse response1 = 



     } catch (IOException e) { // TODO Auto-generated catch block 
     e.printStackTrace(); } 

     } 

     } 

}

錯誤:

02-15 17:45:24.695: E/AndroidRuntime(21890): at android.widget.Toast.<init>(Toast.java:94) 
02-15 17:47:19.343: W/SingleClientConnManager(22288): Invalid use of SingleClientConnManager: connection still allocated. 
02-15 17:47:19.343: W/SingleClientConnManager(22288): Make sure to release the connection before allocating another one. 
+0

發佈的logcat .. –

+2

_「剛完成的應用程序本身的設備通過comunicate‘我的應用程序是stopped.38’_這意味着你的應用程序崩潰。請給我們日誌。同樣在你的構造函數中,它應該是'this.context = context'。 –

+0

@TGMCians 02-15 17:45:24.695:E/AndroidRuntime(21890):\t在android.widget.Toast。 (Toast.java:94) 02-15 17:47:19.343:W/SingleClientConnManager(22288):無效使用SingleClientConnManager的:仍然分配的連接。 02-15 17:47:19.343:W/SingleClientConnManager(22288):請確保在分配另一個連接之前釋放連接。這裏 – user3310467

回答

3

Invalid use of SingleClientConnManager: connection still allocated.

您正在執行的HTTP請求兩次,你使用它之前是完全錯誤的。因此,請刪除第二個httpclient.execute(httppost);,因爲您已經執行了此http請求。

並稱之爲

httpResponse.getEntity().consumeContent(); 

Above method is called to indicate that the content of this entity is no longer required. All entity implementations are expected to release all allocated resources as a result of this method invocation

+0

我改變了這一點,仍然沒有。 – user3310467

+0

你是否再次得到相同的錯誤?如果您已更新,然後更新 –

+0

作品中的代碼!非常感謝 ! – user3310467

0
public static DefaultHttpClient getThreadSafeClient() { 

    DefaultHttpClient client = new DefaultHttpClient(); 
    ClientConnectionManager mgr = client.getConnectionManager(); 
    HttpParams params = client.getParams(); 

    client = new DefaultHttpClient(
     new ThreadSafeClientConnManager(params, 
      mgr.getSchemeRegistry()), params); 

    return client; 
} 

使用此代碼,以便您的免費資源的例外,分配或使用會不會來 此異常可以在兩個或多個線程交互發生單org.apache.http.impl.client.DefaultHttpClient 或者乾脆給新的對象每個請求何時何地,你在呼喚你或交HTTP客戶端請求與服務器獲取數據或下載大文件交互 像

String post_url="http://www.google.com"; 
    DefaultHttpClient client = new DefaultHttpClient(); 
    HttpPost httpost = new HttpPost(Post_url);  
    httpost.setHeader("Accept", "application/json"); 
    httpost.setHeader("Content-type", "application/json"); 
    HttpResponse httpResponse = client.execute(httpost); 
    String response= EntityUtils.toString(httpResponse.getEntity()); 

,你在代碼中要檢索的響應

相關問題