Android实现注册请求

代码仅供参考

Java

package com.example.login_register;

import static baseinfo.Connectinfo.Registerurl;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Register extends AppCompatActivity {
    Button button;
    TextView textView;
    EditText et1,et2,et3,et4,et5,et6,et7;
    String w1,w2,w3,w4,w5,w6,w7;
    RadioButton radioButton;
    RadioGroup radioGroup;
    OkHttpClient okHttpClient;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        radioGroup=findViewById(R.id.rg);
        button=findViewById(R.id.register);
        textView=findViewById(R.id.foot2);
        et1=findViewById(R.id.username);
        et2=findViewById(R.id.password);
        et3=findViewById(R.id.repassword);
        et4=findViewById(R.id.nickname);
        et5=findViewById(R.id.phone);
        et6=findViewById(R.id.email);
        et7=findViewById(R.id.IDcard);
        handler = new Handler(Looper.getMainLooper());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=new Intent(Register.this,Login.class);
                startActivity(intent2);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                w1=et1.getText().toString();
                w2=et2.getText().toString();
                w3=et5.getText().toString();
                w5=et6.getText().toString();
                w6=et7.getText().toString();
                radioButton=findViewById(radioGroup.getCheckedRadioButtonId());
                w4=radioButton.getText().toString();
                Map<String,String> params=new HashMap<>();
                params.put("userName",w1);
                params.put("password",w2);
                params.put("phonenumber",w3);
                params.put("sex",w4);
                params.put("email",w5);
                params.put("idCard",w6);
                String json=new Gson().toJson(params);
                okHttpClient=new OkHttpClient.Builder().build();
                RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
                Request request=new Request.Builder()
                        .post(requestBody)
                        .url(Registerurl)
                        .build();
                Call call=okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final  String data=response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (response!=null&&response.isSuccessful()){
                                    JSONObject jsonObject=null;
                                    try {
                                        jsonObject=new JSONObject(data);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    if (jsonObject.optString("code").equals("200")){
                                        Intent intent1=new Intent(Register.this,Login.class);
                                        startActivity(intent1);
                                    }else {
                                        Toast.makeText(Register.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        });
                    }
                });

            }
        });

    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
<TextView
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="#2196F3"
    android:text="注册"
    android:textSize="30dp"
    android:textColor="@color/white"
    android:gravity="center"
    />

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:hint="请输入用户名"
            />
        <EditText
            android:id="@+id/password"
            android:password="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入密码"
            />
        <EditText
            android:id="@+id/repassword"
            android:password="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入再次密码"
            />
        <EditText
            android:id="@+id/nickname"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入昵称"
            />
        <EditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入手机号"
            />
        <EditText
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入邮箱"
            />
        <EditText
            android:id="@+id/IDcard"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:hint="请输入身份证"
            />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:gravity="center"
            android:text="性别:"
            android:textSize="20dp" />
        <RadioGroup
            android:id="@+id/rg"
            android:layout_toRightOf="@+id/tv"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <RadioButton
                android:id="@+id/rb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:gravity="center"
                android:text="男"/>
            <RadioButton
                android:id="@+id/rb2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20dp"
                android:gravity="center"
                android:text="女"/>
        </RadioGroup>
</RelativeLayout>
    <Button
        android:id="@+id/register"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:background="#2196F3"
        android:layout_marginTop="40dp"
        android:layout_gravity="center"
        android:text="注册"
        android:textSize="20dp"
        android:textColor="@color/white"
        />
    <TextView
        android:id="@+id/foot2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="已有帐号,返回登陆"
        android:textSize="20dp"
        android:textColor="#131314"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        />
</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: ```java // 注册请求 public void register(String username, String password) { try { URL url = new URL("http://example.com/register.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); OutputStream os = conn.getOutputStream(); String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); os.write(data.getBytes()); os.flush(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 注册成功 } else { // 注册失败 } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } // 登录请求 public void login(String username, String password) { try { URL url = new URL("http://example.com/login.php"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); OutputStream os = conn.getOutputStream(); String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); os.write(data.getBytes()); os.flush(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 登录成功 } else { // 登录失败 } conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } } ``` 以上代码中,register() 方法用于发送注册请求,login() 方法用于发送登录请求。其中,需要替换的部分是请求的 URL 和参数,以及请求成功后的处理逻辑。注意,这里使用了 HttpURLConnection 类进行请求

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值