HttpPost:InputDispatcher:“频道无法恢复,将被丢弃!”在Nexus 7上

时间:2014-03-31 09:49:19

标签: java android http-post channel socketchannel

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

E / InputDispatcher:channel' 4273f7b0 ... MainActivity(server)' 〜频道无法恢复,将被处理掉!

人们提到了可能的内存泄漏。见**。但是,当我尝试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;
}

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

有什么想法吗?

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

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

*:这个答案没有完全关联/帮助: App has stopped working Android

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

2 个答案:

答案 0 :(得分:0)

由于某些代码,这是内存链接的常见问题。 查看开发人员文档中的blog post以跟踪它。

答案 1 :(得分:0)

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

在一个简短的句子中,一个主线程可以有很多UI线程做很多工作,但是如果一个子线程(不是主线程)里面有一个UI线程,也许子线程工作完成了,但它的孩子〜 UI线程尚未完成工作,这将导致内存泄漏。

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

供您参考。