java - 无效使用 SingleClientConnManager : connection still allocated

标签 java android android-asynctask

我想知道为什么这段代码不执行?我正在尝试通过 POST 方法从我的设备发送数据,但没有错误。该应用程序通过传达“我的应用程序已停止。”在我的设备上自行完成:

这里是执行:

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. 

最佳答案

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

关于java - 无效使用 SingleClientConnManager : connection still allocated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21800495/

相关文章:

java - 慢速线程处理的缓解技术

android - 带有支持库的 ActionBar 选项卡

android - 如何使用 Intent Android 调用带有特殊字符的号码?

java - 尝试将 setText() 设置为 TextView 时强制关闭应用程序

android - AsyncTask 未在 UI 线程上更新

java - JSONparser 读取句子中的第一个单词

java - Sonar 的 TryStatementTree#resourceList 什么时候返回不是 VariableTree 的 Tree?

java - 在 Sip/VoIP 或常规通话期间收听 DTMF 音

android - 如何根据 VectorDrawables 中的 Material 设计指南为应用程序图标创建渐变长阴影?

java - 具有多个参数的异步函数