Java SingleClientConnManager的使用无效:仍在分配连接
我想知道为什么这个代码不执行?我正在尝试通过POST方法从设备发送数据,但没有错误。应用程序在我的设备上通过通信“我的应用程序已停止”: 以下是执行:Java SingleClientConnManager的使用无效:仍在分配连接,java,android,android-asynctask,Java,Android,Android Asynctask,我想知道为什么这个代码不执行?我正在尝试通过POST方法从设备发送数据,但没有错误。应用程序在我的设备上通过通信“我的应用程序已停止”: 以下是执行: KlientNameValue kn = new KlientNameValue(getApplicationContext()); kn.new MyAsyncTask().execute(zam.klient.getNazwa(),zam.klient.getNip(),zam.klient.getAdres()); 下面是代码: pub
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(); }
}
}
公共类KlientNameValue{
List KlientNameValuePairs=新的ArrayList();
语境;
公共KlientNameValue(上下文){
超级();
this.context=context;
}
公共类MyAsyncTask扩展了AsyncTask{
@重写受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
postData(参数[0],参数[1],参数[2]);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
Toast.makeText(上下文,“Zlecenie zostało wysłane”,
Toast.LENGTH_LONG).show();
}
无效postData(字符串nazwa、字符串nip、字符串adres){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(“这是我的默认链接:)”;
尝试{//添加您的数据
KlientNameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“Kli_imie”,nazwa));
添加(新的BasicNameValuePair(“Kli_adres”,adres));
添加(新的BasicNameValuePair(“Kli_nr_telefonu”),
(nip),;
setEntity(新的UrlEncodedFormEntity(KlientNameValuePairs));
HttpResponse response=httpclient.execute(httppost);
//setEntity(新的UrlEncodedFormEntity(
//ZamovieNameValuePairs));//HttpResponse响应1=
}catch(IOE异常){//TODO自动生成的catch块
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.
02-15 17:45:24.695:E/AndroidRuntime(21890):在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):确保在分配另一个连接之前释放该连接。
SingleClientConnManager的使用无效:仍在分配连接
您在使用http请求之前执行了两次,这是完全错误的。因此,请删除第二个httpclient.execute(httppost);
,因为您已经执行了此http请求
叫这个
httpResponse.getEntity().consumeContent();
调用上述方法表示不再需要此实体的内容。所有实体实现都将释放此方法调用后分配的所有资源
使用此代码,以使您的免费资源和分配或正在使用的例外情况不会出现
当两个或多个线程与单个org.apache.http.impl.client.DefaultHttpClient
或者,只要在您调用的时间和地点为每个请求提供新对象,就可以获取或发布http客户端请求,以便与服务器交互以获取数据或下载大文件
像
正如您喜欢在代码中检索响应一样“只需应用程序通过通信在设备上完成自身”我的应用程序已停止。38“这意味着您的应用程序崩溃。请给我们看看日志。同样在您的构造函数中,它应该是
this.context=context
@TGMCians 02-15 17:45:24.695:E/AndroidRuntime(21890):在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):确保在分配另一个连接之前释放连接。如果在代码块中使用完整的日志猫和格式编辑日志猫,阅读日志猫会容易得多。@codeMagic在这里。我已经更改了,但仍然没有任何更改。您是否再次遇到相同的错误?如果已更新,则更新问题中的代码
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;
}
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());