AndroidHttpClient创建并永不关闭

时间:2014-05-20 10:09:01

标签: android nuxeo

我的Android客户端有问题。 nuxeo上下文包含Context和AndroidHttpClient

我试图尽可能地关闭,但只有方法。 Shutdown(),它阻止我稍后在我的应用程序中询问我的nuxeo服务器

如何关闭它?

堆叠打印

05-20 11:46:06.945: E/AndroidHttpClient(24191): Leak found
05-20 11:46:06.945: E/AndroidHttpClient(24191): java.lang.IllegalStateException: AndroidHttpClient created and never closed
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.net.http.AndroidHttpClient.<init>(AndroidHttpClient.java:158)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.net.http.AndroidHttpClient.newInstance(AndroidHttpClient.java:144)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at org.nuxeo.ecm.automation.client.android.AndroidAutomationClient.<init>(AndroidAutomationClient.java:115>)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at org.nuxeo.android.context.NuxeoContext.getNuxeoClient(NuxeoContext.java:168)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at org.nuxeo.android.context.NuxeoContext.<init>(NuxeoContext.java:110)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at org.nuxeo.android.context.NuxeoContext.<init>(NuxeoContext.java:82)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at com.example.androidnuxeo.MainActivity.onCreate(MainActivity.java:90)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.Activity.performCreate(Activity.java:4562)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.os.Looper.loop(Looper.java:137)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at android.app.ActivityThread.main(ActivityThread.java:4514)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 11:46:06.945: E/AndroidHttpClient(24191): at java.lang.reflect.Method.invoke(Method.java:511)

代码

 NuxeoContext ctx = new NuxeoContext(getBaseApplication());

try {

    Session session = Connection.getSession();

    itemID = ((TwoLineListItem) view).getText2().getText().toString();
    itemTitle =((TwoLineListItem) view).getText1().getText().toString();

    RequeteDetail requete = new RequeteDetail();
    res = requete.execute(itemID,ctx).get();
    type = res.getType();

} catch (Exception e) {
    System.err.println(e);
    e.printStackTrace();
}

NuxeoContext:

public synchronized AndroidAutomationClient getNuxeoClient() {
    while (nuxeoClient != null && shuttingDown) {
        try {
            wait(100);
        } catch (InterruptedException e) {
            // Do nothing
        }
    }
    if (nuxeoClient == null || nuxeoClient.isShutdown()) {
        shuttingDown = false;
       nuxeoClient = new AndroidAutomationClient(
               serverConfig.getAutomationUrl(), androidContext,
                sqlStateManager, blobStore, networkStatus, serverConfig);
        Log.i(TAG, "new Nuxeo client " + nuxeoClient);
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "Call stack: ", new Exception());
        }
    }
    return nuxeoClient;
}

Connection.getSession:

public class Connection extends AsyncTask<NuxeoContext, Void, Session> {

Context ctx;
static Session session;

protected Session doInBackground(NuxeoContext... params) {

    if(session == null || session.getClient().getBaseUrl() == null) {
        NuxeoContext nctx= params[0];
        session = nctx.getSession();    

        System.err.println("create connexion");
    }
    else {
        System.err.println("connexion already open");
    }

   return session;
}

public static Session getSession() {
    if(session == null || session.getClient() == null) {
        System.err.println("No connexion");
    }
    return session;
}
}

我将Nuxeo连接器集成为库,我不使用片段,只使用活动。

1 个答案:

答案 0 :(得分:0)

你是什么意思?

  

如果我不关闭它我的应用程序无法正常工作

您可以致电org.nuxeo.android.context.NuxeoContext.shutdown()关闭nuxeoClient(org.nuxeo.ecm.automation.client.android.AndroidAutomationClient.shutdown())并关闭其AndroidHttpClient ...但您必须只在onDestroy()活动中执行此操作方法,如果没有继承Nuxeo活动。
NuxeoContext自行管理AndroidAutomationClient及其会话。删除这些细节仍然是可能的,但容易出错。

您可以在代码段中添加导入吗?什么是Connection.getSession()

您是如何将Nuxeo Android连接器与您的应用程序集成的:组合或继承?使用Nuxeo活动或片段或您的自定义实现?根据答案,您应该使用以下两种方法之一:

org.nuxeo.android.context.NuxeoContextFactory.getNuxeoContext(Context)
org.nuxeo.android.context.NuxeoContext.get(getApplicationContext())

然后建议这些方法获得NuxeoClientSession

org.nuxeo.android.context.NuxeoContext.getNuxeoClient()
org.nuxeo.android.context.NuxeoContext.getSession()
org.nuxeo.ecm.automation.client.android.AndroidAutomationClient.getSession()

我想我只是理解你的问题:你是否在每个活动中创建了一个新的NuxeoContext?它必须是单身人士。如果是这样,使用NuxeoContextFactory可以解决您的问题,并避免经常关闭它。