qq-jsp登录

// An highlighted block
package com.example.qq_login_fuwuqi;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    EditText editText_name,editText_password;
    ImageButton imageButton;
    String result;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_name=findViewById(R.id.edit_1);
        editText_password=findViewById(R.id.edit_2);
        imageButton=findViewById(R.id.imgBtn);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result="";
                if(editText_name.getText().toString().equals("")||
                        editText_password.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this,"请输入用户名或密码",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    handler=new Handler(){
                        @Override
                        public void handleMessage(@NonNull Message msg) {
                            if("ok".equals(result))
                            {
                                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                                startActivity(intent);
                            }
                            else
                            {
                                Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                            }
                            super.handleMessage(msg);
                        }
                    };
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Send();
                            Message message=handler.obtainMessage();
                            handler.sendMessage(message);
                        }
                    }).start();
                }
            }
        });

    }
    public void Send()
    {
        String address="http://10.211.248.235:8080/android/post.jsp";
        try {
            URL url=new URL(address);
            HttpURLConnection coon= (HttpURLConnection) url.openConnection();
            coon.setRequestMethod("POST");//指定使用post请求
            coon.setDoInput(true);//写入数据
            coon.setDoOutput(true);//读入数据
            coon.setUseCaches(false);//禁止使用缓存
            coon.setInstanceFollowRedirects(true);//自动执行http重定向
            coon.setRequestProperty("Content-type","application/x-www-form-urlencoded");//参数类型设置
            DataOutputStream dos=new DataOutputStream(coon.getOutputStream());
            String param="username="+ URLEncoder.encode(editText_name.getText().toString(),"utf-8")
                    +"&password="+URLEncoder.encode(editText_password.getText().toString(),"utf-8");
            dos.writeBytes(param);//将数据写入流
            dos.flush();
            dos.close();
            if(coon.getResponseCode()==HttpURLConnection.HTTP_OK);
            {
                InputStreamReader isr=new InputStreamReader(coon.getInputStream());
                BufferedReader br=new BufferedReader(isr);
                String data="";
                while((data=br.readLine())!=null)
                {
                    result+=data;
                }
                isr.close();
            }
            coon.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值