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