Android--向服务器提交数据的两种方法Post和Get

本文探讨了在Android应用中实现用户输入验证及通过HTTP POST和GET方法进行网络请求的基本流程,包括错误处理和响应消息展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有些方法用的不是很好,希望大家多多指正

public class MainActivity extends ActionBarActivity {
    EditText et_name;
    EditText et_password;
    Button b_post;
    Button b_get;
    URL url;
    HttpURLConnection conn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText)findViewById(R.id.et_name);
        et_password = (EditText)findViewById(R.id.et_password);
        b_get = (Button)findViewById(R.id.b_get);
        b_post = (Button)findViewById(R.id.b_post);
    }
    public void clik(final View view){
        new Thread(){
            @Override
            public void run() {
                super.run();
                String <span style="font-family:System;">name</span> = et_name.getText().toString();
                String password = et_password.getText().toString();
                if (name.isEmpty()|| password.isEmpty()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                        }
                    });
                    return;
                }
                final String s;
                if (view.getId() == R.id.b_get){
                    s= d<span style="font-family:System;">oGet(name ,password);</span>
                }else {
                    s= do<span style="font-family:System;">Post(name ,password);</span>
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
    }
    public String doPost(String name,String password){
        String u = "http://192.168.2.3:8080/Web/TestServlet";
        String date = "name=" + name + "&password=" + password;
        try {
            url = new URL(u);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            PrintWriter pw = new PrintWriter(conn.getOutputStream());
            pw.print(date);
            pw.flush();
            int code = conn.getResponseCode();
            System.out.println(code);
            if (code == 200){
                InputStream is = conn.getInputStream();
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
                System.out.println(s);
                return s;
            }else{
                return "连接失败...";
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "连接失败...";
        }
    }
    public String doGet(String name,String password){
        String u = "http://192.168.2.3:8080/Web/<span style="font-family: Arial, Helvetica, sans-serif;">TestServlet</span>";
        String date = "?name=" + name + "&password=" + password;
        try {
            url = new URL(u + date);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            int code = conn.getResponseCode();
            if (code == 200){
                InputStream is = conn.getInputStream();
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
                System.out.println(s);
                return s;
            }else{
                return "连接失败...";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "连接失败...";
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石头老师学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值