2
 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 中,此代码不起作用。

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

但是,一些 .php 文件仍然有效。

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

我的代码有问题吗?或者问题出在网络上?

4

0 回答 0