Java HttpPost:InputDispatcher:“;通道已无法恢复,将进行处理&引用;关于Nexus7
在Nexus7(4.3)上,而不是在我的旧设备LG Optimus 3d(Android 2.2)上, 当我做HttpPost时,我得到了这个 E/InputDispatcher﹕ 频道'4273F700。。。MainActivity(服务器)“~通道已不可恢复地断开,将被处置 人们提到了可能的内存泄漏。见**。然而,当我尝试HttpPost时,这个问题会在启动时立即发生。它仍然可能是内存泄漏吗 下面是我如何编写HttpPost的:Java HttpPost:InputDispatcher:“;通道已无法恢复,将进行处理&引用;关于Nexus7,java,android,http-post,channel,socketchannel,Java,Android,Http Post,Channel,Socketchannel,在Nexus7(4.3)上,而不是在我的旧设备LG Optimus 3d(Android 2.2)上, 当我做HttpPost时,我得到了这个 E/InputDispatcher﹕ 频道'4273F700。。。MainActivity(服务器)“~通道已不可恢复地断开,将被处置 人们提到了可能的内存泄漏。见**。然而,当我尝试HttpPost时,这个问题会在启动时立即发生。它仍然可能是内存泄漏吗 下面是我如何编写HttpPost的: public void server_addUserGetId
public void server_addUserGetId()
{
String url = GS.baseUrl() + "/users";
HttpPost theHttpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("dId", s_UserInfo.getInstance().m_device_id ));
try {
theHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpPostAsync theHttpPostAsync = new HttpPostAsync(new OnPostExecuteHandler() {
@Override
public void handlePostExecute(Object oHttpResponse) {
HttpResponse theHttpResponse = (HttpResponse) oHttpResponse;
JSONObject jo = GS.getJSONObject(theHttpResponse.getEntity());
try {
s_UserInfo.getInstance().m_user_id = jo.getString("_id");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
theHttpPostAsync.execute(theHttpPost);
return;
}
public void服务器\u addUserGetId()
{
字符串url=GS.baseUrl()+“/users”;
HttpPost theHttpPost=新的HttpPost(url);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“dId”,s_UserInfo.getInstance().m_device_id));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
HttpPostAsync theHttpPostAsync=新建HttpPostAsync(新建OnPostExecuteHandler(){
@凌驾
公共无效handlePostExecute(对象oHttpResponse){
HttpResponse HttpResponse=(HttpResponse)ohttpreponse;
JSONObject jo=GS.getJSONObject(httpresponse.getEntity());
试一试{
s_UserInfo.getInstance().m_user_id=jo.getString(“_id”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
});
httppostsync.execute(httppost);
返回;
}
以下是我的HttpPostAsync任务:
public class HttpPostAsync extends AsyncTask<HttpPost, Integer, HttpResponse>
{
private HttpPost m_HttpPost;
private HttpResponse m_HttpResponse;
private OnPostExecuteHandler m_OnPostExecuteHandler;
public HttpPostAsync(OnPostExecuteHandler listener)
{
m_OnPostExecuteHandler = listener;
}
protected HttpResponse doInBackground(HttpPost ... args)
{
m_HttpPost = args[0];
if(GS.dl>5) Log.d("GRA: HttpPostAsync", "doInBackground: Thread.currentThread().getId()=" + Thread.currentThread().getId());
m_HttpResponse = visit(m_HttpPost);
return m_HttpResponse;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
if(GS.dl>5) Log.d("GRA: HttpPostAsync", "onPostExecute: Thread.currentThread().getId()=" + Thread.currentThread().getId());
if(GS.dl>5) Log.d("GRA: HttpPostAsync", "onPostExecute: result=" + result);
//if(GS.dl>5) Log.d("GRA: HttpPostAsync", "onPostExecute: m_HttpEntity="+m_HttpEntity);
m_OnPostExecuteHandler.handlePostExecute(m_HttpResponse);
}
public HttpResponse visit(HttpPost theHttpPost)
{
HttpResponse response = null;
try {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Post Request
response = httpclient.execute(theHttpPost);
} catch (IOException e) {
e.printStackTrace();
Log.d("HttpPostAsync.java", "IOException e=" + e);
// TODO Auto-generated catch block
}
return response;
}
}
公共类HttpPostAsync扩展异步任务
{
私有HttpPost m_HttpPost;
私人HttpResponse mu HttpResponse;
私有OnPostExecuteHandler m_OnPostExecuteHandler;
公共HttpPostAsync(OnPostExecuteHandler侦听器)
{
m_OnPostExecuteHandler=侦听器;
}
受保护的HttpResponse doInBackground(HttpPost…args)
{
m_HttpPost=args[0];
如果(GS.dl>5)Log.d(“GRA:HttpPostAsync”,“doInBackground:Thread.currentThread().getId()=”+Thread.currentThread().getId());
m_HttpResponse=访问(m_HttpPost);
返回m_HttpResponse;
}
受保护的void onProgressUpdate(整数…进度){
}
受保护的void onPostExecute(长结果){
如果(GS.dl>5)Log.d(“GRA:HttpPostAsync”,“onPostExecute:Thread.currentThread().getId()=”+Thread.currentThread().getId());
如果(GS.dl>5)Log.d(“GRA:HttpPostAsync”,“onPostExecute:result=“+result”);
//if(GS.dl>5)Log.d(“GRA:HttpPostAsync”,“onPostExecute:m_HttpEntity=“+m_HttpEntity”);
m_OnPostExecuteHandler.handlePostExecute(m_HttpResponse);
}
公众HttpResponse访问(HttpPost-theHttpPost)
{
HttpResponse响应=null;
试一试{
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
//执行HTTP Post请求
response=httpclient.execute(httppost);
}捕获(IOE异常){
e、 printStackTrace();
Log.d(“HttpPostAsync.java”,“IOException e=“+e”);
//TODO自动生成的捕捉块
}
返回响应;
}
}
有什么想法吗
我读到一个SO答案*它可能与ArrayList初始化有关,所以我也尝试过这样初始化ArrayList中的1,但问题仍然存在:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
List name-valuepairs=new-ArrayList(1);
*:因此,回答不完全相关/有帮助的问题:
**内存泄漏相关 由于某些代码,这是内存链接的常见问题。
请查看开发人员文档中的内容以找到它。我在两天前遇到了同样的问题,关于Android,而不是纯Java,这个asnwer只供您参考。 我现在解决了。我是台湾人,我很高兴在这里再次回答。 你用过UI新线程吗?不要重复使用UI新线程看起来像三明治。它会导致内存泄漏 简而言之,一个主线程可以有多个UI线程来做很多工作,但是如果一个子线程(不是主线程)拥有一个UI线程,可能子线程的工作已经完成,但是它的子线程~UI线程还没有完成工作,这将导致内存泄漏。 例如,对于片段和UI应用程序……这将导致内存泄漏。
getActivity().runOnUiThread(new Runnable(){
public void run() {
ShowDataScreen();
getActivity().runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(getActivity(), "This is error way",Toast.LENGTH_SHORT).show();
}});// end of No.2 UI new thread
}});// end of No.1 UI new thread
我的解决方案重新排列如下:
getActivity().runOnUiThread(new Runnable(){
public void run() {
ShowDataScreen();
}});// end of No.1 UI new thread
getActivity().runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(getActivity(), "This is correct way",Toast.LENGTH_SHORT).show();
}});// end of No.2 UI new thread
供您参考。当我尝试HttpPost时,此问题会在启动时立即发生。它仍然可能是内存泄漏吗?无法读取您的代码,但gc不会收集匿名类的实例,而不管其线程如何?