Xutils框架之网络请求

时间:2021-03-27 22:16:24

Post 请求:

final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
RequestParams requestParams = new RequestParams();
requestParams.addBodyParameter("username",username);
        requestParams.addBodyParameter("password",password);
        if(TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)){
        Toast.makeText(LoginActivity.this,"请填写账号和密码",Toast.LENGTH_SHORT).show();
        break;
        }
        HttpUtils httpUtils=new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST,url_path,requestParams,new RequestCallBack<String>(){
public void onSuccess(ResponseInfo<String>responseInfo){
        String result=responseInfo.result;
        if("1".equals(result)){
        AppPrefs.putSharedString(LoginActivity.this,"login_key","登录保存");
        startActivity(new Intent(LoginActivity.this,MainActivity.class));
        finish();
        }else{
        Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
        }
        }

public void onFailure(HttpException e,String s){

        }
        });

Get 请求:

      HttpUtils httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET, get_url_path, new RequestCallBack<String>() {
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        if ("1".equals(result)) {
                            AppPrefs.putSharedString(LoginActivity.this, "login_key", "登录保存");
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }

                    public void onFailure(HttpException e, String s) {
                    }
                });