Android端通过网络通信调用后台接口初步实现注册

一、后台字段和返回的json信息
请求字段
请求字段 字段含义 是否必填
username 用户名 是
password 用户密码 是
nickname 用户昵称 是

应答报文

{
“status” :0,
“msg” :“注册成功”
}
业务异常
{
“status” :1,
“msg” :“用户已存在”
}
{
“status” :1,
“msg” :“昵称已存在”
}

系统异常
{
“status” : 999,
“msg” :“系统出现异常,请联系管理员”
}

二、android 布局文件编写





<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/widget_size_10"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/widget_size_40"
        android:text="账号:"
        android:textSize="@dimen/font_size_18"
        android:textColor="@color/white"
        />
    <EditText
        android:id="@+id/et_user"
        android:layout_width="@dimen/widget_size_178"
        android:layout_height="wrap_content"
        android:textColor="#fcfdfc"
        android:inputType="text"
        android:textCursorDrawable="@null"
        android:textSize="@dimen/font_size_16"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/widget_size_10"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/widget_size_40"
        android:text="密码:"
        android:textSize="@dimen/font_size_18"
        android:textColor="@color/white"
        />
    <EditText
        android:id="@+id/et_pass"
        android:layout_width="@dimen/widget_size_178"
        android:layout_height="wrap_content"
        android:textColor="#fcfdfc"
        android:inputType="text"
        android:textCursorDrawable="@null"
        android:textSize="@dimen/font_size_16"/>
</LinearLayout>
<LinearLayout
    android:layout_width="@dimen/widget_size_160"
    android:layout_height="@dimen/widget_size_40"
    android:gravity="center_horizontal"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/widget_size_20">
    <Button
        android:id="@+id/bt_register"
        android:layout_width="match_parent"
        android:layout_height="@dimen/widget_size_26"
        android:background="@drawable/button_shape"
        android:onClick="registerPOST"
        android:text="提交"
        android:textColor="#fcfdfc"
        android:textSize="@dimen/font_size_16"/>
</LinearLayout>

三、AndroidManifest.xml权限配置
加入

四、java
private EditText nick,user,pass;
private Button register;
HttpClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_register);
    client = new DefaultHttpClient();
    nick = findViewById(R.id.et_nick);
    user = findViewById(R.id.et_user);
    pass = findViewById(R.id.et_pass);
    register = findViewById(R.id.bt_register);
    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        RegisterUser();
        }
    });
}
@Override
public void onClick(View v) {
    switch (v.getId()) {


    }
}
public  void readNet(String url,String user,String pass,String nick){
    new AsyncTask<String,Void,String>(){
        @Override
        protected String doInBackground(String... arg0) {
            String urlString =arg0[0];
            HttpPost post = new HttpPost(urlString);
            try {
                List<BasicNameValuePair> list =new ArrayList<>();
                list.add(new BasicNameValuePair("username",arg0[1]));
                list.add(new BasicNameValuePair("password",arg0[2]));
                list.add(new BasicNameValuePair("nickname",arg0[3]));
                post.setEntity(new UrlEncodedFormEntity(list));
            }catch (UnsupportedEncodingException e){ }
            try{
                HttpResponse response = client.execute(post);
                String value = EntityUtils.toString(response.getEntity());
                return  value;
            }catch (ClientProtocolException e1){
                e1.printStackTrace();
            }catch (IOException e1){
                e1.printStackTrace();
            }
            return  null;
        }
        @Override
        protected void onPostExecute(String result) {
            JSONObject jsonObject = JSONObject.parseObject(result);
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("msg");

                if (status.equals("0")) {
                    showToast(msg);
                } else if (status.equals("1")) {
                    showToast(msg);
                } else if (status.equals("999")) {
                    showToast(msg);
                }
        }
    }.execute(url,user,pass,nick);
}

private void RegisterUser() {
    String nk = nick.getText().toString();
    String us = user.getText().toString();
    String ps = pass.getText().toString();
    OkHttpClient mOkHttpClient = new OkHttpClient();
    FormEncodingBuilder bulider = new FormEncodingBuilder();
    if (TextUtils.isEmpty(nk) && TextUtils.isEmpty(us) && TextUtils.isEmpty(ps)) {
        showToast("请填写你的信息");
        return;
    }
    if (TextUtils.isEmpty(nk)){
        showToast("请填写你的昵称");
        return;
    }
    if (TextUtils.isEmpty(us)){
        showToast("请填写你的账号");
        return;
    }
    if (TextUtils.isEmpty(ps)){
        showToast("请填写你的密码");
        return;
    }
    else {
    //AppConfig.BASE_USER_REGISTER为我在另一文件写的固定常量后台接口路劲
        readNet(AppConfig.BASE_USER_REGISTER,user.getText().toString(),pass.getText().toString(),nick.getText().toString());
    }
}
public void showToast(String text){
    Toast.makeText(Register.this,text,Toast.LENGTH_SHORT).show();
}

}
程序执行成功

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值