Android 中传递 json 的问题

问答小助手 2013-05-13 01:54:28
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/2520

问题描述:

我在开发一个程序,在 webpage 上使用jquery。
$.post(url, {param: paramstring}, function(result){});


根据参数结构,Paramstring 是一个 json字符串,如:{"action":"get","username":"username"}
现在我想在android中运行,再在页面上添加两个textview 来输入用户名和密码。也有一个注册按钮。按钮监听程序:
EditText et1 = (EditText)findViewById(R.id.username);
String user = et1.getText().toString();
EditText et2 = (EditText)findViewById(R.id.pass);
String password = et2.getText().toString();
// the password should upload after MD5 encryption. this is encryption method. the result is the same with js encryption.
String password_md5 = toMd5(password.getBytes());
Log.d(TAG, user+"-"+password+"-"+password_md5);
try {
HttpPost request = new HttpPost(URL);
JSONObject params = new JSONObject();
params.put("action", "get");
params.put("result", "user");
params.put("category", "base");
params.put("username", user);
params.put("password", password_md5);

List<BasicNameValuePair> sendData = new ArrayList<BasicNameValuePair>();
sendData.add(new BasicNameValuePair("param", params.toString()));

System.out.println(params.toString());

request.setEntity(new UrlEncodedFormEntity(sendData,"utf-8"));
System.out.println(EntityUtils.toString(request.getEntity()));

HttpResponse response= new DefaultHttpClient().execute(request);
String retSrc = EntityUtils.toString(response.getEntity());

System.out.println(retSrc);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


上面的代码返回数据显示登录错误,我觉得是因为 json 结构的问题。{param:paramstr} in $.post()方法是一个map。我改了好几次还是错误的。
什么问题呢?

解决方案:

你应该分别传递每个参数,不需要一个 json 结构。 jQuery 使用的 JSON 结构只是 $.post() 方法中一个可变数目的参数。
把你代码中的这部分:
params.put("action", "get");
params.put("result", "user");
params.put("category", "base");
params.put("username", user);
params.put("password", password_md5);

List<BasicNameValuePair> sendData = new ArrayList<BasicNameValuePair>();
sendData.add(new BasicNameValuePair("param", params.toString()));


改为:

List<BasicNameValuePair> sendData = new ArrayList<BasicNameValuePair>();
sendData.add(new BasicNameValuePair("action", "get"));
sendData.add(new BasicNameValuePair("result", "user"));
sendData.add(new BasicNameValuePair("category", "base"));
sendData.add(new BasicNameValuePair("username", user));
sendData.add(new BasicNameValuePair("password", password_md5));



用sendData list 取代 JSON 对象。
...全文
1161 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
小立子的世界 2013-06-14
  • 打赏
  • 举报
回复
不了接JSON,不过可以把JSON解析出来,用参数的方式去传递。
u010771426 2013-05-20
  • 打赏
  • 举报
回复
同问··顶下这个帖··
Zh_java_2009 2013-05-17
  • 打赏
  • 举报
回复
我同事好像也正在处理类似的东西 我叫他来看看。
leexiaoxi_007 2013-05-17
  • 打赏
  • 举报
回复
buliuzhenming 2013-05-16
  • 打赏
  • 举报
回复
顶一下这个问题
mbugaifc 2013-05-16
  • 打赏
  • 举报
回复
u010688537 2013-05-14
  • 打赏
  • 举报
回复
这个我也真不懂,求解释 !!!!!!
a82344626 2013-05-14
  • 打赏
  • 举报
回复
HTTPAnalyzerFullV6 HttpWatchPro-v6.014 这2个是抓http get 或 post方便比较直观 Charles 这个是抓aMF方式 WpePro1.0c wireshark-win32-1.2.10 这2个是抓Socket 以前回别人的~
a82344626 2013-05-14
  • 打赏
  • 举报
回复
用个数据抓取工具看发送登陆的时候的数据 然后再用HTTPPOST或HTTPGET或httpconnection 直接发送字符串 其实JSON也是一串字符串来的~ 也可以在模拟器看抓下看你发送的数据是不是和网页上的发送的一样~
IT修补匠 2013-05-14
  • 打赏
  • 举报
回复
可以能参数方式不一样 params.put("action", "get"); params.put("result", "user"); params.put("category", "base"); params.put("username", user); params.put("password", password_md5); List<BasicNameValuePair> sendData = new ArrayList<BasicNameValuePair>(); sendData.add(new BasicNameValuePair("param", params.toString())); 最后的 url http://xxxx.com?param="action:get,result...." List<BasicNameValuePair> sendData = new ArrayList<BasicNameValuePair>(); sendData.add(new BasicNameValuePair("action", "get")); sendData.add(new BasicNameValuePair("result", "user")); sendData.add(new BasicNameValuePair("category", "base")); sendData.add(new BasicNameValuePair("username", user)); sendData.add(new BasicNameValuePair("password", password_md5)); 最后的 url http://xxxx.com?action=get&result=user
wangchen19931113 2013-05-13
  • 打赏
  • 举报
回复
这个我也不懂,求解释

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧