Runnable Handler

来源:互联网 发布:淘宝小号购买实名认证 编辑:程序博客网 时间:2024/06/12 08:39
线程  和   操作通讯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;@Overrideprotected 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() {@Overridepublic 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(){@Overridepublic 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 truepublic 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 falseprivate void loginFalseDoing()    {//new AlertDialog.Builder(this).setTitle("确认").setMessage("确定吗?").setNegativeButton("",null).show();AlertDialog dialog =new AlertDialog.Builder(this)                .setTitle("错误")                .setMessage("用户名或密码错误")                .setPositiveButton("确定",null)                .show();}}
0 0
原创粉丝点击