首页 > 代码库 > 开发日志—详细的链接服务器代码(登录)
开发日志—详细的链接服务器代码(登录)
进行链接服务器 ,输入密码账号,登录页面 是基本APP都有的功能,总结了下:过
用户登陆界面
/** * @author think *用户登陆界面 */ public class LoginUi extends Activity implements HttpCallBack{ private EditText emailET; private EditText passwordET; private ImageButton loginIB; private TextView registerTV; private TextView pwdForgetTV; private User user; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_login); initViews(); setListener(); } <span style="background-color: rgb(255, 204, 255);"> private void setListener() { loginIB.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { UserLogin userlogin=null; try { userlogin = (UserLogin) BeanFactory.getInstance("UserLogin"); } catch (Exception e) { BasicUtil.showMassage(LoginUi.this, "sorry, class not found"); } getUserData(); if(user==null){ return; } userlogin.loginResult(user,LoginUi.this); } });</span> registerTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startActivity(RegisterUi.class); } }); pwdForgetTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startActivity(FindPasswordUi.class); } }); } /** * 初始化组件 */ private void initViews() { emailET=(EditText)findViewById(R.id.et_login_email); passwordET=(EditText)findViewById(R.id.et_login_password); loginIB = (ImageButton)findViewById(R.id.ib_login_submit); registerTV = (TextView)findViewById(R.id.tv_login_register); pwdForgetTV=(TextView) findViewById(R.id.tv_login_forgetpassord); // TODO 初始化组件 } /** * 获得用户的登录数据 */ private void getUserData() { String username=emailET.getText().toString(); String password=passwordET.getText().toString(); if(TextUtils.isEmpty(username)){ BasicUtil.showMassage(this, "邮箱不能为空"); return; } if(TextUtils.isEmpty(password)){ BasicUtil.showMassage(this, "密码不能为空"); return; } user=new User(username, password); } private void startActivity(Class<?> clazz){ Intent intent=new Intent(this,clazz); startActivity(intent); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { return super.onMenuItemSelected(featureId, item); } @Override protected void finalize() throws Throwable { super.finalize(); } @Override <span style="background-color: rgb(255, 204, 255);"> public void callBack(Object object) { Message message = handler.obtainMessage(); message.what = 1; message.obj = object; handler.sendMessage(message); } Handler handler=new Handler(){ @Override public void handleMessage(android.os.Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: System.out.println("结果:"+ msg.obj);//这里就是你得到的结果 可以再这里修改UI if((Boolean)msg.obj==true){ startActivity(MainUi.class); }else{ BasicUtil.showMassage(LoginUi.this, "登录失败"); } break; default: break; } } };</span> }
回调函数接口HttpCallBack
public interface HttpCallBack { void callBack(Object object); }
用户登录业务类UserLogin
/** * @author think 用户登录业务类 */ public class UserLogin { /** * 用户登录业务 * * @param userinfo * @return 登录结果 */ public void loginResult(User userinfo, HttpCallBack httpCallBack) { ArrayList<NameValuePair> pairs = getPairs(userinfo); String uri = "http://" + ConstValue.IP + ":" + ConstValue.PORT; try { ApacheHttpClient.httpPost(uri, pairs, httpCallBack); } catch (Exception e) { e.printStackTrace(); } } /** * @param userinfo * 封装用户登录信息 */ private ArrayList<NameValuePair> getPairs(User userinfo) { ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); NameValuePair pair = null; pair = new BasicNameValuePair("username", userinfo.getEmail()); pairs.add(pair); pair = new BasicNameValuePair("password", userinfo.getPassword()); pairs.add(pair); return pairs; } }
发送Http请求ApacheHttpClient
/** * @author think 发送Http请求 */ public class ApacheHttpClient { /** * @return * */ public static String httpGet(String uri) { String response = null;// 响应 HttpClient httpClient = new DefaultHttpClient(); // 创建HttpGet对象 HttpGet httpGet = new HttpGet(uri); HttpResponse httpResponse; try { // 使用execute方法发送HTTP GET请求,并返回HttpResponse对象 httpResponse = httpClient.execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode();// 返回码 // , if (statusCode == HttpStatus.SC_OK) { // 获得返回结果 response = EntityUtils.toString(httpResponse.getEntity()); } else { response = "返回码:" + statusCode; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(response); return response; } /** * 以Post方式发送请求 * * @param url * 请求地址 * @param params * 参数 ,Post方式必须用NameValuePair[]阵列储存参数 * @return * @throws Exception */ <span style="background-color: rgb(255, 204, 255);">public static void httpPost(final String uri, final List<NameValuePair> params,final HttpCallBack httpCallBack) throws Exception { new Thread(new Runnable() { public void run() { String response = null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(uri); try { // 设置httpPost请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 使用execute方法发送HTTP Post请求,并返回HttpResponse对象,会有延迟等待 HttpResponse httpResponse = httpClient.execute(httpPost); int statusCode = httpResponse.getStatusLine() .getStatusCode();// 返回码 , if (statusCode == HttpStatus.SC_OK) { response = EntityUtils.toString(httpResponse .getEntity()); JSONObject obj = new JSONObject(response); Boolean result = (Boolean) obj.opt("success"); Log.i("----", "result=" + result); httpCallBack.callBack(result);//回调 } else { response = "返回码:" + statusCode; } } catch (Exception e) { e.printStackTrace(); } } }).start(); // get-->opt // ToDo:解析obj } }</span>
开发日志—详细的链接服务器代码(登录)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。