在过去的几周里,我一直在与android做斗争,并为自己开发了第一个工作应用程序.不幸的是只在我的模拟器上。我真的希望有人能帮我让它在我的android设备上工作。模拟器运行v2.2,我的设备2.3+。
我有一个单例的‘网站服务’类,它是由一个扩展“应用程序”的类创建的。and站点服务类创建1个线程安全连接管理器,并包含一个“CookieStore”。每个HTTPGet/Post创建一个新的HTTPClient,并将threadsafeconn.manager附加到它。每次我做一个帖子或得到,我试着附上饼干,如果有。当登录到网站的安全部分时,cookie只设置一次。与EntitityUtils.ToString和Jsoup一起,我解析了我的“安全”网站部分的每个HTML结果。在登录时,我在android中创建了一个sqllite表,其中只存储了一个简单的记录,即登录的用户。
当我在我的仿真器上运行这个程序时,所有的事情都会像我所希望的那样运行,但是在我的设备上,似乎出了很大的问题。我把调试器连到手机上了。登录似乎进行得很好,曲奇正在设置中。由于我似乎没有犯错误,所以这部分方伊利石可能也很好。在加载配置文件页面时,我可以看到HTTPGet操作获得了附加于CookieStore的cookie,但是我收到的HTML/doc是登录页面的95%。
我说95%,因为我确实在我根深蒂固的欲望HD上访问过我的个人资料页面一两次,但在另一个愿望HD上我没有,我的感觉XE也没有。当我加载配置文件页面时(一个带有hashmap作为输入的自定义列表框),我无法启动任何操作。他们马上就中止了,很可能他们也有同样的问题(结果是获得登录页面)。
是否有任何权限我忘记了,是否有什么我应该检查,或者你知道它可能是什么?我现在真的很困惑。
小更新
我刚重启了我的手机。当我启动程序时,当我第一次登录时,我的配置文件页面就会被加载。我的“细节”行动似乎奏效了。此操作不连接到internet,它只显示存储在类中的信息。我的其他两个操作(复制和删除)不起作用(结果可能是“登录页面”)。当我转动我的屏幕(方向改变),页面‘重新启动’,它试图再次加载配置文件页。列表框被清除,并且我没有得到我的配置文件页面(这是一个广告列表)。
更新2-08-03我的清单文件只包含互联网权限,除了我的活动注册。一个小的“奇怪”的额外的。看来我的“备份”代码与我在这里发布的代码几乎相等。每次它似乎都附在我的曲奇饼上,但它仍然没有用它做任何事。另外,我昨天在玩手机的时候,我改变了几次语言(自动重新加载活动),突然,我有一两次把我的个人资料放在前面。我不能对它做任何操作,但是我的课程中包含的信息是可以访问和填充的。PS。刚才上午8:15我的时间,我可以再次加载我的配置文件后,切换方向4-5次.
Update08-03-2012我一直很困惑,晚上尝试了很多东西,现在我甚至把我的仿真器版本带回了1,得到了备份,所以np,但我认为我没有正确地使用我的http-客户端。我删除了前面的所有代码,这是我使用的当前代码,用于我的'websiteservice',以及我在每个get/post上实例化的'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之类的。希望明天我能用清晰的头脑再次解决这个问题。
感谢您的想法和意见!
发布于 2012-03-08 18:16:13
我已经解决了这个非常棘手的问题,并将在下面发布解决方案。不幸的是,我不知道确切的原因,但这肯定是一种综合因素。
与每次使用连接管理器创建DefaultHttpClient不同,我只创建了1 DefaultHttpClient,并且一直重复使用。而且,每次我做一个帖子或get时,我都会创建一个新的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;
}我不是百分之百肯定,但我想我甚至可以省略setCookies的部分。在我的情况下,cookies是一个私有声明的CookieStore,我在帖子后填写它。
最后一个令人讨厌的部分是,旋转时,它重新创建了活动。为了解决这个问题,我扩展了扩展'Application‘的类。在这门课中,我列出了我的对象列表。在我的“OnCreate”中,我检查对象列表是否存在,如果可用的话使用该列表。在注销或刷新时,我重新创建该列表。诀窍就是这几行:(我忽略了我在‘’类中添加的部分):
if(((ApplicationInstance)getApplication()).getProfile() == null)
this.initProfileItems();
else
{
this.ProfileItems =((ApplicationInstance)getApplication()).getProfile();
this.initListView();
}ApplicationInstance是我的类,扩展了应用程序。我的“initList”处理对象。我的initProfileItems会得到一个实际的数据:-)
希望我给某人留了一个头痛的机会:),谢谢你的时间,@androidnoob!
https://stackoverflow.com/questions/9597164
复制相似问题