在使用HttpUrlConnection的ICS会话cookie错误中

时间:2012-05-29 12:06:50

标签: android session-cookies android-4.0-ice-cream-sandwich httpurlconnection

 MyApplication app = (MyApplication) mContext.getApplicationContext();

 CookieManager cookie = CookieManager.getInstance();                

    if(cookie.getCookie(mContext.getString(R.string.host_url))!=null){
    conn.setRequestProperty("Cookie",cookie.getCookie(mContext.getString(R.string.host_url)));                  app.cookie=cookie.getCookie(mContext.getString(R.string.host_url));
            }

  Map m = conn.getHeaderFields();
            if (m.containsKey("Set-Cookie")) {
                String cookies = "";
                Collection c = (Collection) m.get("Set-Cookie");
                for (Iterator i = c.iterator(); i.hasNext();) {
                    cookies += (String) i.next() + ",";
                }
                cookie.setCookie(mContext.getString(R.string.host_url), cookies);
                Log.d("tag", "cookie set " +  cookies);
            }                   

-------------------使用cookie进行会话--------------------

          if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
                String line = null;
                String res = "";
                while ((line = reader.readLine()) != null) {
                    res += line;
                }
                Log.d("tag", "url : " + requestUrl + "\n" + res);

                jobj = new JSONObject(res);
                if (jobj.getString("result").equals("YES")) {
                    success = true;
                } else if (jobj.getString("result").equals("NO")) {
                    result = jobj.getString("reason");
                }

                reader.close();
            }else{
                result = "HTTP FAILED";
            }

--------------在网络上获得回复---------------------

在ICS中,此代码不起作用。

在网络中连接一些.php时,会产生会话错误 或其他.php在web中,conn.getResponseCode()== HttpURLConnection.HTTP_OK无法捕获响应。(result =“HTTP FAILED”;这种情况)

但是,某些.php文件仍在使用。

在android 2.2或2.3中运行良好,但4.0.4 NexusS,NexusGalaxy遇到了麻烦。

我的代码有问题吗?或问题是网络?

0 个答案:

没有答案