我有一个服务器,它向我的Android应用程序发送一个会话cookie,用于认证通信.我正在尝试使用指向同一服务器的URL加载WebView,并且我正在尝试传递会话cookie以进行身份验证.我观察到它间歇性地工作,但我不知道为什么.我使用相同的会话cookie在我的服务器上进行其他调用,这些调用永远不会失败.我只是在尝试在WebView中加载URL时才发现此问题,并且每次都不会发生这种情况.很沮丧.
下面是我用来执行此操作的代码.任何帮助将不胜感激.
String myUrl = ""http://mydomain.com/";
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
Cookie sessionCookie = getCookie();
if(sessionCookie != null){
String cookieString = sessionCookie.getName() +"="+sessionCookie.getValue()+"; domain="+sessionCookie.getDomain();
cookieManager.setCookie(myUrl, cookieString);
CookieSyncManager.getInstance().sync();
}
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(myUrl);
Run Code Online (Sandbox Code Playgroud) 我有一个模型,其中数据在呈现为json时永远不应包含在内.所以我实现了类'as_json方法以适当地运行.问题是当与此模型关联的其他模型呈现json时,我的自定义as_json未被调用.
class Owner < ActiveRecord::Base
has_one :dog
def as_json(options={})
puts "Owner::as_json"
super(options)
end
end
class Dog < ActiveRecord::Base
belongs_to :owner
def as_json(options={})
puts "Dog::as_json"
options[:except] = :secret
super(options)
end
end
Run Code Online (Sandbox Code Playgroud)
加载开发环境(Rails 3.0.3)
ruby-1.9.2-p136:001> d = Dog.first
=#<Dog id: 1, owner_id: 1, name: "Scooby", secret: "I enjoy crapping everwhere">>
ruby-1.9.2-p136:002> d.as_json
Dog :: as_json
=> {"dog" => {"id"=> 1,"name"=>"Scooby","owner_id"=> 1}}
ruby-1.9.2-p136:004> d.owner.as_json(:include =>:dog)
所有者:: as_json
=> {"owner"=> {"id"=> 1,"name"=>"Shaggy",:dog => {"id"=> 1,"name"=>"Scooby", "owner_id"=> 1,"秘密"=>"我喜欢疯狂到处"}}}}
谢谢您的帮助
我的笔记本电脑崩溃了,当它重新启动时,"cmd /"(切换注释)被打破了.当我尝试在仅包含"foo"的行上切换注释时,我在代码而不是"#foo"中获得此输出:
/tmp/temp_textmate.2erfLj:68:in/bin/bash:-c:第0行:在寻找匹配的
'' /bin/bash: -c: line 1: syntax error: unexpected end of filemap' for " foo":String (NoMethodError) from /tmp/temp_textmate.2erfLj:48:in /bin/bash: -c: line 0: unexpected EOF while looking for matching''/ bin/bash时出现意外EOF :-c:第2行:语法错误:意外结束文件'
这让我疯了