Java HttpPost:InputDispatcher:“通道不可恢复地损坏,将被处理!” 在 Nexus 7 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22758482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:44:43  来源:igfitidea点击:

HttpPost: InputDispatcher: "Channel is unrecoverably broken and will be disposed!" on Nexus 7

javaandroidhttp-postchannelsocketchannel

提问by mylord

On Nexus 7 (4.3), and not on my older device, LG Optimus 3d (Android 2.2), when I do HttpPost, I get this

在 Nexus 7 (4.3) 上,而不是在我的旧设备 LG Optimus 3d (Android 2.2) 上,当我执行 HttpPost 时,我得到了这个

E/InputDispatcher﹕ channel '4273f7b0 ... MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

E/InputDispatcher: channel '4273f7b0 ... MainActivity (server)' ~ 通道不可恢复地损坏,将被处理!

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?

人们提到了可能的内存泄漏。看 **。但是,当我尝试 HttpPost 时,这个问题会在启动时立即发生。它仍然可能是内存泄漏吗?

Here is how I'm doing the HttpPost:

这是我如何做 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:

这是我的 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;
    }
}

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:

我读了一个 SO answer* 它可能与 ArrayList 初始化有关,所以我也尝试过这样初始化,在 ArrayList 中使用 1,但问题仍然存在:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

*: SO answer that didn't totally relate/help: App has stopped working Android

*:不完全相关的答案/帮助: 应用程序已停止运行 Android

** memory leak related? http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html

** 内存泄漏相关?http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html

回答by Solution

This is the common issue of memory link due to some code. Look at blog postin the developer docs to track it down.

由于某些代码,这是内存链接的常见问题。查看开发人员文档中的博客文章以对其进行跟踪。

回答by Hymanie_Hung

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.

我在两天前遇到了同样的麻烦,Android,不是纯Java,这个答案仅供参考。我现在解决了。我是台湾人,很高兴再次在这里回答。你有没有使用过 UI 新线程?不要重复使用 UI 新线程看起来像三明治。它应该会导致内存泄漏。

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.

简而言之,一个主线程可以有很多UI线程来做很多工作,但是如果一个子线程(不是主线程)在里面拥有一个UI线程,也许子线程工作完成了,但是它的孩子~UI线程没有完成工作,这会导致内存泄漏。

For example...for Fragment & UI application...this will cause memory leaks.

例如...对于 Fragment & 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

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.

供你参考。