NetClient的post函数:
/**
* json post方式请求调用方法 返回为json
*
* @param url 请求地址
* @param params 请求参数 可以为空
* @param res 必须实现此类 处理成功失败等 回调
*/
public void post(String url, RequestParams params, final JsonHttpResponseHandler res) {
System.out.println("请求URL:" + url);
if (!NetUtil.checkNetWork(context)) {
Utils.showLongToast(context, Constants.NET_ERROR);
return;
}
try {
if (params != null) {
client.post(url, params, res);
} else {
client.post(url, res);
}
} catch (Exception e) {
// TODO
e.printStackTrace();
}
}
其中
JsonHttpResponseHandler所在jar包:JunsNetHelper.jar,
使用方法:
RequestParams params = new RequestParams();
params.put("username", userName);
params.put("password", DES.md5Pwd(password));
getLoadingDialog("正在登录... ").show();
netClient.post(Constants.Login_URL, params, new BaseJsonRes() {
@Override
public void onMySuccess(String data) {
Utils.putValue(LoginActivity.this, Constants.UserInfo, data);
Utils.putBooleanValue(LoginActivity.this, Constants.LoginState, true);
Utils.putValue(LoginActivity.this, Constants.NAME, userName);
Utils.putValue(LoginActivity.this, Constants.PWD, DES.md5Pwd(password));
getChatService(userName, DES.md5Pwd(password));
}
@Override
public void onMyFailure() {
getLoadingDialog("正在登录").dismiss();
}
});