2024/05/06

今日学习时长:2小时

代码行数:150行

博客数量:1篇

今天主要在想该如何将Android和springboot来实现前后端分离测试,所以今天主要在测试该如何用Android想spring boot发送网络请求。

package com.example.pwvs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.pwvs.R;
import com.example.pwvs.activity.pojo.User;
import com.example.pwvs.activity.util.LoginRequest;

public class Home_Activity extends AppCompatActivity {
    private EditText username_v;
    private EditText password_v;

    private Button login_v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        init();
        login_v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String username=username_v.getText().toString();
                        String password=password_v.getText().toString();
                        User user =new User();
                        user.setUsername(username);
                        user.setPassword(password);
                        LoginRequest loginRequest=new LoginRequest();
                        String url="http://10.99.124.205:8082/login";
                        loginRequest.post(url,user);
                    }
                }).start();
            }
        });
    }
    private void init()
    {
        username_v=findViewById(R.id.username);
        password_v=findViewById(R.id.password);
        login_v=findViewById(R.id.login);
    }
}

 

package com.example.pwvs.activity.util;

import com.example.pwvs.activity.pojo.User;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class LoginRequest {
    public void post(String url1, User user) {
        try {
            URL url = new URL(url1);
            HttpURLConnection Connection = (HttpURLConnection) url.openConnection();//创建连接
            Connection.setRequestMethod("POST");
            Connection.setConnectTimeout(3000);
            Connection.setReadTimeout(3000);
            Connection.setDoInput(true);
            Connection.setDoOutput(true);
            Connection.setUseCaches(false);
            Connection.setInstanceFollowRedirects(true);
            // 设置使用标准编码格式编码参数的名-值对
            Connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            Connection.connect();

            String params = "username=" + user.getUsername() + "&password=" + user.getPassword();

            OutputStream out = Connection.getOutputStream();

            out.write(params.getBytes());
            out.flush();
            out.close();

            String msg = "";


            int responseCode = Connection.getResponseCode();
            if (responseCode == Connection.HTTP_OK) {//判断请求是否成功
                BufferedReader reader = new BufferedReader(new InputStreamReader(Connection.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    msg += line + "\n";
                }
                reader.close();
            }
            Connection.disconnect();
            System.out.println(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

package com.example.pwvs.activity.pojo;

public class User {
    private Integer id;
    private String username;
    private String password;

    public User() {
    }

    public User(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

 

posted @ 2024-05-06 23:19  伐木工熊大  阅读(2)  评论(0编辑  收藏  举报