私のアプリでこのソースコードを使用する場合、最初は問題はありませんが、wifi をオフにして Web サービスからリソースを取得しようとすると、アプリは次の状態で例外をスローします: Leak Found 、私は httpclient の close メソッドを使用しますしかし、何も変わらず、このコードを使用して 1 日 2 回作業すると、例外がスローされます。
スタック印刷:
10-17 16:42:00.524: ERROR/AndroidHttpClient(931): Leak found
10-17 16:42:00.524: ERROR/AndroidHttpClient(931): java.lang.IllegalStateException: AndroidHttpClient created and never closed
コード :
public String getRequest(String url, ArrayList<NameValuePair> nameValuePairs){
String resp = "";
HttpParams httpParameters = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
AndroidHttpClient httpclient = null;
try {
httpclient = AndroidHttpClient.newInstance("V");
httpclient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream instream = entity.getContent();
Reader reader = new InputStreamReader(instream, HTTP.UTF_8);
StringBuilder buffer = new StringBuilder("");
char[] tmp = new char[1024];
int l;
while ((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
reader.close();
resp = buffer.toString().replace(""", "\"");
Log.d("V", "RESPONSE:-----\n" + resp + "\n--------------");
} catch (IOException e){
Log.e("V IOException [Send Request]","IO Exception");
if((e != null) && (e.getMessage() != null)){
Log.e("V",e.getMessage().toString());
}
} catch (Exception e) {
Log.e("V Exception [Send Request]","Exception Requester");
if((e != null) && (e.getMessage() != null)){
Log.e("V",e.getMessage().toString());
}
}finally {
if (httpclient != null && httpclient.getConnectionManager() != null) {
httpclient.close();
httpclient = null;
}
}
return resp;
}