get用户登录模板

    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
        protected static final int SUCCESS = 1;
        protected static final int ERROR = 2;
        private EditText et_qq;
        private EditText et_pwd;
        private TextView tv_status;

        private Handler handler = new Handler(){
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                case ERROR:
                    Toast.makeText(MainActivity.this, "请求失败,请检查网络和服务器的状态", 0).show();
                    break;
                case SUCCESS:
                    Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
                    tv_status.setText("当前状态:"+ (String)msg.obj);
                    break;
                }
            };
        };

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et_qq = (EditText) findViewById(R.id.et_qq);
            et_pwd = (EditText) findViewById(R.id.et_pwd);
            tv_status = (TextView) findViewById(R.id.tv_status);
        }
        public void login(View view){
            //http://localhost:8080/web/LoginServlet?qq=10000&pwd=abcde
            final String qq = et_qq.getText().toString().trim();
            final String pwd = et_pwd.getText().toString().trim();
            if(TextUtils.isEmpty(qq)||TextUtils.isEmpty(pwd)){
                Toast.makeText(this, "qq或者密码不能为空", 0).show();
                return;
            }
            new Thread(){
                public void run() {
                    try {
                        String path = "http://192.168.1.103:8080/web/LoginServlet?qq="+URLEncoder.encode(qq, "utf-8")+"&pwd="+URLEncoder.encode(pwd, "utf-8");
                        URL url = new URL(path);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(5000);
                        int code = conn.getResponseCode();
                        if(code == 200){
                            InputStream is = conn.getInputStream();
                            String result = StreamTools.readStream(is);
                            Message msg = Message.obtain();
                            msg.what = SUCCESS;
                            msg.obj = result;
                            handler.sendMessage(msg);
                        }else{
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            handler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message msg = Message.obtain();
                        msg.what = ERROR;
                        handler.sendMessage(msg);
                    }
                };
            }.start();
        }
    }

 

转载于:https://my.oschina.net/hlxxjava/blog/787161

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值