在过去的几周里,我一直在与 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 左右。希望明天我能再次清醒地解决这个问题。
想法和意见表示赞赏!