(Android) 似乎我的 JSON 查询正在进行双重编码
我的 Android 应用程序遇到一些奇怪的错误。看来这段代码对 JSON 字符串进行了双重编码。应该发送的是 ?{"email":"[电子邮件受保护] ","密码":"asdf"} 或者 ?%7B%22email%22:%22.....
服务器看到的是 %257B%2522email%2522:%2522 .... 这意味着服务器看到%7B%22email%22:%22 .....
这使服务器感到困惑。
有什么想法为什么会发生这种情况吗?
感谢您的帮助
//编辑以更好地定义对象
代码:
DefaultHttpClient c = new DefaultHttpClient();
if(cookies!=null)
c.setCookieStore(cookies);
JSONObject jso = new JSONObject():
if(loginNotLogout){
jso.put("email", "[email protected]");
jso.put("password", "PassW0RD");
}
URI u = null;
if(loginNotLogout)
u= new URI("HTTP","www.website.com","/UserService",jso.toString(),"");
else
u= new URI("HTTP","www.website.com","/UserService",jso.toString(),"");
HttpGet httpget = new HttpGet(u);
HttpResponse response = c.execute(httpget);
ret.jsonString = EntityUtils.toString(response.getEntity());
I am getting some weird errors with my Android App. It appears that this code is double encoding the JSON string. What should be sent is ?{"email":"[email protected]","password":"asdf"}
or
?%7B%22email%22:%22.....
what the server is seeing is %257B%2522email%2522:%2522 ....
which means the server sees %7B%22email%22:%22 .....
This confuses the server.
Any ideas why this is happening?
Thank you for your help
//edited to define objects better
Code:
DefaultHttpClient c = new DefaultHttpClient();
if(cookies!=null)
c.setCookieStore(cookies);
JSONObject jso = new JSONObject():
if(loginNotLogout){
jso.put("email", "[email protected]");
jso.put("password", "PassW0RD");
}
URI u = null;
if(loginNotLogout)
u= new URI("HTTP","www.website.com","/UserService",jso.toString(),"");
else
u= new URI("HTTP","www.website.com","/UserService",jso.toString(),"");
HttpGet httpget = new HttpGet(u);
HttpResponse response = c.execute(httpget);
ret.jsonString = EntityUtils.toString(response.getEntity());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么是
userData
?您是否从任何 EditText 获取值?
将
getText().toString()
与 EditText 中的文本一起使用会有帮助吗?what is
userData
?are you getting values from any EditText?
will using
getText().toString()
with the text from EditText help?事实证明,放弃了“www”。 URI 构造函数中的权限字段导致地址字符串被正确编码。
我不是网络专家,但如果有人能解释这一点,我会洗耳恭听。或在这种情况下的眼睛。
--
安德鲁
As it turns out, dropping the "www." from the authority field in the URI constructor caused the address string to be encoded correctly.
I am no web expert, but if anyone can explain this I am all ears. or eyes in this case.
--
Andrew