Runnable Handler

线程  和   操作通讯

package com.ningbo.c21.nbxt;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Login extends Activity {
private EditText login_name;
    private EditText login_paw;
    private Button login_but;
    private Handlerhandler=null;
HttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
client = new DefaultHttpClient();

login_name = (EditText)findViewById(R.id.login_name);
login_paw = (EditText)findViewById(R.id.login_paw);
login_but = (Button)findViewById(R.id.login_button);


login_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//                readNet("http://www.c21ningbo.com.cn/APP_Houseinfo/Login.asmx/HasUser", login_name.getText().toString(), login_paw.getText().toString());
//                readNet("http://www.c21ningbo.com.cn/APP_Houseinfo/LoginMethod.ashx?method=HasUser", login_name.getText().toString(), login_paw.getText().toString());
//                Login.this.finish();
                //new Thread(networkTask).start();

                //开一条子线程加载网络数据
Runnable runnable=new Runnable()
                {
public void run() {
// 在这里进行 http request.网络请求相关操作
HttpPost post = new HttpPost("http://www********");
String mess = "";
                        try {
                            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("userid", login_name.getText().toString()));
list.add(new BasicNameValuePair("pwd", login_paw.getText().toString()));
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse response = client.execute(post);
mess = EntityUtils.toString(response.getEntity());

}
catch (Exception ex)
                        {
                            ex.printStackTrace();
}
//发送消息,并把mess结合对象传递过去
handler.sendMessage(handler.obtainMessage(0, mess)); //关键字是0
}
                };


                new Thread(runnable).start();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
                        String mymess = msg.obj.toString();
                        if(mymess.equals("错误") || mymess == null || mymess.equals("")){
                                    loginFalseDoing();
}
else {
                                    loginTrueDoing(mymess);
}
                    }
                };

//                try
//                {
//                    //开启线程
//                    new Thread(runnable).start();
//                    //handler与线程之间的通信及数据处理
//                    handler=new Handler()
//                    {
//                        public void handleMessage(Message msg)
//                        {
//                            if(msg.what == 0)
//                            {
//                                //msg.obj是获取handler发送信息传来的数据
//                                @SuppressWarnings("unchecked")
//                                //ArrayList<Person> person=(ArrayList<Person>) msg.obj;
//                                        //给ListView绑定数据
//                                        String mygetstring = (String) msg.obj;
//                                if(mygetstring.equals("错误") || mygetstring == null || mygetstring.equals("")){
//                                    loginFalseDoing();
//                                }
//                                else {
//                                    loginTrueDoing(mygetstring);
//                                }
//                            }
//                        }
//                    };
//                }
//                catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
}
        });





}

//login true
public void loginTrueDoing(String UserName){
        SharedPreferences sharedPreferences = getSharedPreferences("mySP", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", UserName);
editor.commit();
Login.this.finish();
}

//login false
private void loginFalseDoing()
    {
//new AlertDialog.Builder(this).setTitle("确认").setMessage("确定吗?").setNegativeButton("",null).show();
AlertDialog dialog =new AlertDialog.Builder(this)
                .setTitle("错误")
                .setMessage("用户名或密码错误")
                .setPositiveButton("确定",null)
                .show();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值