1
我正在开发一个项目。我写一篇关于网页使用jQuery:Android提供json
$.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}
在$.post()
方法是一个映射。我改变了很多次,这仍然是错误的。
你能给我一些建议吗?非常感谢!
我建议你在服务器端查看jQuery发送的内容和android发送的内容(并发布这些结果)。然后我建议你使用更加标准的认证方式(http有内置的机制) – njzk2