首页 > 程序开发 > web前端 > HTML/CSS >

android网络请求,OKHttppost访问webAPI

2017-07-14

android网络请求,OKHttppost访问webAPI。

android客户端

//region 登录处理Post
/**
 * 登录处理Post
 */
public static void loginHandlePost(final Handler handler, final String username, final String password) {

    final Message msg = Message.obtain();
    try {
        
        String urlStr = "URL地址";

	//请求参数
        FormBody formBody = new FormBody.Builder()
                .add("userName", username)
                .add("password", password)
                .build();

//sendOkHttpRequestPost封装后回调

        HttpUtils.sendOkHttpRequestPost(urlStr, formBody, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String msg_str = "登录失败,服务器未响应";
                msg.what = 6;
                msg.obj = msg_str;
                handler.sendMessage(msg);
                LogUtils.v(TAB, msg_str);
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                int code = response.code();
                if (code == 200) {
                    String content = response.body().string();
                    Gson gson = new Gson();
                    AdminDoctorAndNurse adminDoctorAndNurse = gson.fromJson(content, AdminDoctorAndNurse.class);
                    if (adminDoctorAndNurse != null) {
                        if (adminDoctorAndNurse.getIsEnable() == 1) {                          
                            String msg_str = "登录成功";
                            msg.what = 200;
                            msg.obj = adminDoctorAndNurse;
                            handler.sendMessage(msg);
                            LogUtils.v(TAB, msg_str);

                        } else if (adminDoctorAndNurse.getIsEnable() == 0) {
                            String msg_str = "当前用户不可用,请联系管理员";
                            msg.what = 3;
                            msg.obj = msg_str;
                            handler.sendMessage(msg);
                            LogUtils.v(TAB, msg_str);
                        }
                    } else {
                        String msg_str = "登录失败,用户名或密码错误";
                        msg.what = 4;
                        msg.obj = msg_str;
                        handler.sendMessage(msg);
                        LogUtils.v(TAB, msg_str);
                    }
                } else {
                    String msg_str = "访问失败!" + code;
                    msg.what = 5;
                    msg.obj = msg_str;
                    handler.sendMessage(msg);
                    LogUtils.v(TAB, msg_str);
                }
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}
//endregion

Web API端

///

/// 用户远程登录
///

///
///
/// 
[HttpPost]
public Admin_DoctorAndNurse ClientLoginPost([FromBody]User obj)
{
Model.Admin_DoctorAndNurse model = new Admin_DoctorAndNurse();
if (GlobalClass.DataConnectionIsEnable() == true)
{

#region 登录验证
string tryUserName = obj.userName.Trim();
string tryUserPwd = obj.password.Trim();
if (tryUserName != "" && tryUserName != "")
{
BLL.Admin_DoctorAndNurse bll = new BLL.Admin_DoctorAndNurse();
model = bll.GetModel(tryUserName, tryUserPwd, true);
}
#endregion

return model;
}

return null;
}

注意:User 对象源码如下:

public class User
{

public string userName { get; set; }
public string password { get; set; }
}

相关文章
最新文章
热点推荐