问题
On Nexus 7 (4.3), and not on my older device, LG Optimus 3d (Android 2.2), when I do HttpPost, I get this
E/InputDispatcher﹕ channel '4273f7b0 ... MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
People have mentioned a possible memory leak. See **. However, this problem happens right away on startup when I try the HttpPost. Is it still likely a memory leak?
Here is how I'm doing the HttpPost:
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;
}
Here is my HttpPostAsync task:
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;
}
}
Any ideas?
I read on an SO answer* it might have to do with the ArrayList initialization, so I've also tried initializing like this, with 1, in the ArrayList, but the problem persists:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
*: SO answer that didn't totally relate/help: App has stopped working Android
** memory leak related? http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html
回答1:
This is the common issue of memory link due to some code. Look at blog post in the developer docs to track it down.
回答2:
I met same troubles in 2 days ago for Android, not pure Java, this asnwer let you reference only. I solve it now.I am Taiwanese, I am glad to answer here once more. Do you ever use UI new thread? Do not double use UI new thread look like sandwich. It should cause memory leaks.
in a short sentence, a main thread could have many UI threads to do many work, but if one sub thread(not main thread) own a UI thread inside, maybe sub thread work done, but its kid ~ UI thread has not finish work, this will case memory leaks.
For example...for Fragment & UI application...this will cause memory leaks.
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
My solution is rearrange as below:
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
for you reference.
来源:https://stackoverflow.com/questions/22758482/httppost-inputdispatcher-channel-is-unrecoverably-broken-and-will-be-disposed