0

在过去的几周里,我一直在与 android 作斗争,并让自己成为了第一个可以工作的应用程序……不幸的是,我只是在我的模拟器上。我真的希望有人可以帮助我让它在我的 android 设备上运行。模拟器运行 v2.2,我的设备 2.3+。

我有一个单例“网站服务”类,它是由扩展“应用程序”的类创建的。网站服务类创建 1 个线程安全连接管理器,并包含一个“CookieStore”。每个 HTTPGet/Post 都会创建一个新的 HTTPClient,并附加了 threadsafeconn.manager。每次我发布或获取信息时,我都会尝试附加 cookie(如果有)。cookie 仅在登录网站的安全部分时设置。与 EntityUtils.ToString 和 Jsoup 一起,我解析了我的“安全”网站部分的每个 HTML 结果。登录时,我在 android 中创建了一个 sqllite 表,其中只存储了 1 条简单记录,即登录的用户。

当我在模拟器上运行它时,一切都按照我的意愿运行,但在我的设备上,似乎出现了严重错误。我将调试器连接到我的手机。登录似乎很顺利,并且正在设置 cookie。sqllite 位可能也很好,因为我似乎没有出错。加载配置文件页面后,我可以看到 HTTPGet 操作获取的是 CookieStore 附加的 cookie,但我收到的 HTML 结果/文档是登录页面的 95%。

我说 95%,因为我确实在我的根深蒂固的 Desire HD 上访问过我的个人资料页面一两次,但在另一个 Desire HD 上我没有,在我的 Sensation XE 上也没有。我确实加载了配置文件页面(带有哈希图作为输入的自定义列表框)的时间,我无法开始任何操作。他们立即中止了,可能他们遇到了同样的问题(结果是登录页面)。

有没有我忘记的权限,有什么我应该检查的,或者你知道它可能是什么吗?我现在真的很困惑。

小更新
我刚刚重新启动了我的手机。当我启动程序时,我第一次登录时,我的个人资料页面被加载。我的“细节”行动似乎奏效了。此操作不连接到互联网左右,它只是显示我存储在一个类中的信息。我的其他 2 个操作(复制和删除)不起作用(结果我可能会得到“登录页面”)。当我转动屏幕(方向改变)时,页面“重新启动”并尝试再次加载个人资料页面。列表框被清除,我没有找回我的个人资料页面(这是一个广告列表)。

更新 2 - 08-03 我的清单文件只包含互联网权限,除了我的活动注册。一个小的“奇怪”额外。看来我的“备份”代码几乎等于我在此处发布的代码。每次它似乎都附加了我的cookie,但它仍然没有做任何事情:(。另外,我昨天在玩手机,我改变了几次方向(它会自动重新加载活动),突然间,我有一次或两次将我的个人资料放在前面。我无法对其进行任何操作,但我的包含信息的课程可以访问并填充。PS. 刚刚我的时间是早上 8.15,我可以在切换后再次加载我的个人资料定向4-5次。

2012 年 8 月 3 日更新 我一直在困惑,晚上尝试了很多东西,现在我什至把我的模拟器版本恢复到了 1 号。得到了备份,所以 np,但我想我没有使用我的 http -客户端正确。我删除了所有以前的代码,这是我用于“网站服务”的当前代码,以及我在每次获取/发布时实例化的“Webclient”,只需附加连接管理器即可。

private static WebsiteService instance;

    ThreadSafeClientConnManager manager;
    HttpContext localContext;
    CookieStore cookies;
    public String returnCode = "";
    public HttpPost httpPost = null;
    public HttpGet httpGet = null;

    public static WebsiteService getInstance()
    {
        // Return the instance
        return instance;
    }

    private WebsiteService()
    {
        DefaultHttpClient client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();
        manager = new ThreadSafeClientConnManager(params,mgr.getSchemeRegistry());
        localContext = new BasicHttpContext();
        cookies = new BasicCookieStore();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookies);
    }

    public static void initInstance()
    {
        if (instance == null)
            instance = new WebsiteService();
    }

    public WebsiteClient postPage(String url, List<NameValuePair> nvps)
    {
        return postPage(url, nvps, true);
    }

    public WebsiteClient getPage(String url) {

        WebsiteClient client = new WebsiteClient(manager);
        DefaultHttpClient httpClient = client.httpClient;

        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
        //HttpProtocolParams.setContentCharset(httpClient.getParams(), "CP1250");

        if(cookies != null)
            httpClient.setCookieStore(cookies);

        httpGet = new HttpGet(url);

        try {
            client.response = httpClient.execute(httpGet,localContext);
        } catch (ClientProtocolException e) {
            System.out.println("HTTPService : ClientProtocolException : "+e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("HTTPService : IOException : "+e);
            return null;
        }

        //cookies = httpClient.getCookieStore();
        return client;
    }


    public WebsiteClient postPage(String url, List<NameValuePair> nvps, boolean autoRedirect) {

        WebsiteClient client = new WebsiteClient(manager);
        DefaultHttpClient httpClient = client.httpClient;
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        if(cookies != null)
            httpClient.setCookieStore(cookies);

        if(!autoRedirect)
        {
            HttpParams params = httpClient.getParams();
            HttpClientParams.setRedirecting(params, false);
        }

        httpPost = new HttpPost(url);
        client.response = null;
//
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " +
                "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)");
        httpPost.setHeader("Accept", "text/html,application/xml," +
                "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            if(nvps != null)
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.ISO_8859_1));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
        }
        //httpPost.setEntity(tmp);

        try {
            client.response = httpClient.execute(httpPost, localContext);
        } catch (ClientProtocolException e) {
            System.out.println("HTTPService : ClientProtocolException : "+e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("HTTPService : IOException : "+e);
            return null;
        } 

        cookies = httpClient.getCookieStore();
        return client;
    }
}

我的网站客户端:

public DefaultHttpClient httpClient;
public HttpResponse response = null;

public WebsiteClient(ThreadSafeClientConnManager manager)
{
    DefaultHttpClient client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    httpClient = new DefaultHttpClient(manager, params);
}

我一定是在 cookie 上做错了,或者不应该使用相同的 Httpget/Httppost 左右。希望明天我能再次清醒地解决这个问题。

想法和意见表示赞赏!

4

1 回答 1

1

我已经能够解决我遇到的这个非常尴尬的问题,并将在下面发布解决方案。不幸的是,我不知道确切的原因,但这肯定是多种因素的结合。

我没有每次用我的连接管理器再次创建一个 DefaultHttpClient,而是只创建了1 个 DefaultHttpClient,我一直重复使用它。此外,每次我发布或获取信息时,我都会创建一个新的 HTTPGet/HTTPPost,而不是从现有的重用/重新创建它。

我的网站服务类的最终构造函数:

private WebsiteService()
{
    CookieManager.getInstance().setAcceptCookie(true);
    httpclient = new DefaultHttpClient();
    ClientConnectionManager mgr = httpclient.getConnectionManager();
    HttpParams params = httpclient.getParams();
    manager = new ThreadSafeClientConnManager(params,mgr.getSchemeRegistry());
    localContext = new BasicHttpContext();
    cookies = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookies);
}

一个简单的“GetPage”调用现在看起来像这样:

httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        if(cookies != null)
            httpClient.setCookieStore(cookies);

        HttpGet httpGet = new HttpGet(url);

        try {
            client.response = httpClient.execute(httpGet,localContext);
        } catch (ClientProtocolException e) {
            System.out.println("HTTPService : ClientProtocolException : "+e.getMessage());
            return null;
        } catch (IOException e) {
            System.out.println("HTTPService : IOException : "+e);
            return null;
        }

我不是 100% 确定,但我认为我什至可以省略 setCookies 位。在我的情况下,cookie 是一个私有声明的 CookieStore,我在发布后填写。

最后一个令人讨厌的部分是,在旋转时,它会重新创建活动。为了解决这个问题,我扩展了扩展“应用程序”的类。在这堂课中,我列出了我的对象。在我的“OnCreate”中,我检查对象列表是否存在,并在可用时使用该列表。注销或刷新后,我会重新创建该列表。诀窍就是这几行:(我省略了我在“扩展应用程序”类中添加的部分):

    if(((ApplicationInstance)getApplication()).getProfile() == null)
        this.initProfileItems();
    else
    {
        this.ProfileItems =((ApplicationInstance)getApplication()).getProfile(); 
        this.initListView();
    }

ApplicationInstance 是我的类,它扩展了 Application。我的“initList”处理对象。我的 initProfileItems 确实获取了实际数据:-)

希望我让别人头疼:),谢谢你的时间,@androidnoob!

于 2012-03-08T18:16:13.153 回答