Android网络编程-Thread和Handler(三)

             客户端:Thread和Handler

MainActivity.java

package com.example.justyu.login_demo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.justyu.login_demo.Sercice.ServiceException;
import com.example.justyu.login_demo.Sercice.UserService;
import com.example.justyu.login_demo.Sercice.UserServiceImp;

import java.lang.ref.WeakReference;


public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    private EditText loginName,loginPassword;
    private Button loginButton;
    private  static final int login_success=1;
    private  static final String  MSG_LOGIN_ERROR="登陆失败!";
    private  static final String  MSG_LOGIN_SUCCESS="登陆成功!";
    public   static final String  msg_login_failed="登录名或密码错误!!";
    private static ProgressDialog dialog;
    //接口 的实现
    private UserService userService=new UserServiceImp();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginName=(EditText)this.findViewById(R.id.editText);
        loginPassword=(EditText)this.findViewById(R.id.editText2);
        loginButton=(Button)this.findViewById(R.id.button);
        loginButton.setOnClickListener(this);

    }

   //点击登陆按钮 后进行的操作
    @Override
    public void onClick(View v) {
       final String Name=loginName.getText().toString().trim();
       final String Password=loginPassword.getText().toString().trim();
        /**
         * 登陆验证
         */
        if(dialog==null){
            dialog=new ProgressDialog(MainActivity.this);
            dialog.setTitle("等待");
            dialog.setMessage("登录中。。。。。");
            dialog.setCancelable(true);
            dialog.show();
        }

        //副线程
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                  userService.userLogin(Name,Password);
                    //发送消息
                  handler.sendEmptyMessage(login_success);
            }//业务逻辑异常
                catch (ServiceException e){
                    e.printStackTrace();
                    //发送消息
                    Message msg=new Message();
                    Bundle data=new Bundle();
                    data.putSerializable("ERROR_MSG",e.getMessage());
                    msg.setData(data);
                    //传过去的是0
                    handler.sendMessage(msg);
                }
                catch (Exception e){
                    e.printStackTrace();
                    //发送消息
                    Message msg=new Message();
                    Bundle data=new Bundle();
                    data.putSerializable("ERROR_MSG",MSG_LOGIN_ERROR);
                    msg.setData(data);
                    //传过去的是0
                    handler.sendMessage(msg);
                }
            }
        });
        thread.start();
    }
    private void showTip(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();

    }
    /**
     * 主线程与副线程之间的通信 Handler
     */
   private static class MyHandler extends Handler{
        private final WeakReference<Activity> myActivity;
        public MyHandler(MainActivity activity){
            myActivity=new WeakReference<Activity>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            //可以引用MainActivity的
            // 方法引用((MainActivity)myActivity.get()).
            if(dialog!=null){
                dialog.dismiss();
            }

            int flag=msg.what;
            switch (flag){
                case 0:
                    String errorMsg=(String)msg.getData().getSerializable("ERROR_MSG");
                    ((MainActivity)myActivity.get()).showTip(errorMsg);
                    break;
                case login_success:
                    ((MainActivity)myActivity.get()).showTip(MSG_LOGIN_SUCCESS);
                    break;
                default:
                    break;
            }
        }
    }
    private MyHandler handler=new MyHandler(this);
}

userService.java

package com.example.justyu.login_demo.Sercice;


/**

 * Created by JustYu on 2015/4/9.

 */

public interface UserService {

    public void userLogin(String loginName,String loginPassword) throws Exception;



}


UserServiceImp.java

package com.example.justyu.login_demo.Sercice;

import android.util.Log;

import com.example.justyu.login_demo.MainActivity;

/**
 * Created by JustYu on 2015/4/9.
 */
public class UserServiceImp implements UserService {
    @Override
    public void userLogin(String loginName, String loginPassword) throws Exception {
        Log.d("Yu",loginName);
        Log.d("Yu",loginPassword);
        Thread.sleep(3000);
        //与服务器端交互
        if(loginName.equals("1")&&loginPassword.equals("1")){

        }else{
         throw  new ServiceException(MainActivity.msg_login_failed);
        }
    }
}


你可能感兴趣的:(android,网络编程)