get用户登录模板
博客专区 > hlxxjava 的博客 > 博客详情
get用户登录模板
hlxxjava 发表于1年前
get用户登录模板
  • 发表于 1年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

    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();
        }
    }

 

共有 人打赏支持
粉丝 2
博文 7
码字总数 1481
×
hlxxjava
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: