一、前言

在空闲之余,学学新东西

二、服务端的代码编写与部署

这里采取的方式是MVC+EF返回Json数据,(本来是想用Nancy来实现的,想想电脑太卡就不开多个虚拟机了,用用IIS部署也好)

主要是接受客户端的登陆请求,服务器端返回请求的结果

这里的内容比较简单不在啰嗦,直接上代码了:

 using System.Linq;
using System.Web.Mvc;
namespace Catcher.AndroidDemo.EasyLogOn.Service.Controllers
{
public class UserController : Controller
{
public ActionResult LogOn(string userName, string userPwd)
{
bool result = IsAuth(userName,userPwd);
ReturnModel m = new ReturnModel();
if (result)
{
m.Code = "";
m.Msg = "Success";
}
else
{
m.Code = "";
m.Msg = "Failure";
}
return Json(m, JsonRequestBehavior.AllowGet);
}
public bool IsAuth(string name, string pwd)
{
using (Models.DBDemo db = new Models.DBDemo())
{
int count = db.UserInfo.Count(u=>u.UserName==name&&u.UPassword==pwd);
return count == ? true : false;
}
}
}
public class ReturnModel
{
public string Code { get; set; }
public string Msg { get; set; }
}
}

发布,测试一下是否可行

OK

三、客户端(Android)的编码实现

既然是登录,肯定有两个文本框和一个登陆按钮啦~

登录之后又要有什么呢,显示一下欢迎就够了,放一个TextView

下面就来布局一下(左边是Main.axml,右边是User.axml)

      

具体的布局代码如下:

Main.axml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="80px"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayoutForName">
<TextView
android:text="姓名:"
android:layout_width="81.5dp"
android:layout_height="match_parent"
android:id="@+id/textViewName"
android:textAllCaps="true"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center" />
<EditText
android:layout_width="291.0dp"
android:layout_height="match_parent"
android:id="@+id/txtName" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="80px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearLayoutForName"
android:layout_marginTop="20dp"
android:id="@+id/linearLayoutForPwd">
<TextView
android:text="密码:"
android:layout_width="81.5dp"
android:layout_height="match_parent"
android:id="@+id/textViewPwd"
android:textAllCaps="true"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center" />
<EditText
android:layout_width="291.0dp"
android:layout_height="match_parent"
android:id="@+id/txtPwd"
android:inputType="textPassword" />
</LinearLayout>
<Button
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@id/linearLayoutForPwd"
android:id="@+id/btnLogin"
android:textAllCaps="true"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center" />
</RelativeLayout>

User.axml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvInfo"
android:minHeight="60dp"
android:gravity="center"
android:textSize="20dp" />
</LinearLayout>

主要是是相对布局与线性布局的结合

布局好了,就该编写实现代码了!!

MainActivity.cs

主要就是接收用户的输入,进行判断和校验,通过的就跳转到下一页面。这里面还用到了一点网络请求。

因为是演示,所以是一大堆代码。。。

里面有用到json的解析,用的是Newtonsoft.Joson,当然,写法不规范,不要吐槽。

 using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
namespace Catcher.AndroidDemo.EasyLogOn
{
[Activity(Label = "简单的登录Demo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
EditText myName = FindViewById<EditText>(Resource.Id.txtName);
EditText myPwd = FindViewById<EditText>(Resource.Id.txtPwd);
Button login = FindViewById<Button>(Resource.Id.btnLogin);
login.Click += delegate
{
string name = myName.Text;
string pwd = myPwd.Text;
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd))
{
Toast.MakeText(this, "请输入用户名和密码!!", ToastLength.Long).Show();
return;
}
else
{
string loginUrl = string.Format("http://192.168.1.102:8077/User/LogOn?userName={0}&userPwd={1}", name, pwd);
var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(loginUrl));
var httpRes = (HttpWebResponse)httpReq.GetResponse();
if (httpRes.StatusCode == HttpStatusCode.OK)
{
string result = new StreamReader(httpRes.GetResponseStream()).ReadToEnd();
result = result.Replace("\"", "'");
ReturnModel s = JsonConvert.DeserializeObject<ReturnModel>(result);
if (s.Code == "")
{
var intent = new Intent(this, typeof(UserActivity));
intent.PutExtra("name", name);
StartActivity(intent);
}
else
{
Toast.MakeText(this, "用户名或密码不正确!!", ToastLength.Long).Show();
return;
}
}
}
};
}
}
public class ReturnModel
{
public string Code { get; set; }
public string Msg { get; set; }
}
}

下面就是跳转之后的页面了,就是从MainActivity传过来的用户名显示在TextView那里。

 using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
namespace Catcher.AndroidDemo.EasyLogOn
{
[Activity(Label = "用户首页")]
public class UserActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.User);
TextView info = FindViewById<TextView>(Resource.Id.tvInfo);
string name = Intent.GetStringExtra("name");
info.Text = name + "欢迎您的到来!" ;
}
}
}

然后就OK了,是不是也很Easy呢。

下面来看看效果

什么都不输入的时候,输入错误的时候,输入正确的时候

         

如果想生成apk文件的话,需要将模式调整为Release模式!!!

四、总结对比

跟原生的Android开发(Java)相比,有很多相似的地方也有很多细节区别,适应就好。

做这个Demo的时候,编写界面的时候貌似没发现有智能提示,不知道有没有处理的好办法!

需要注意的是,这个Demo在数据的传输过程中并没有进行加解密的处理,这是不可取的!

其他的话,就两个字的感觉,方便。

最后附上这个Demo的代码:

https://github.com/hwqdt/Demos/tree/master/src/Catcher.AndroidDemo

Xamarin.Android再体验之简单的登录Demo的更多相关文章

  1. Xamarin.Android之封装个简单的网络请求类

    一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...

  2. Xamarin.Android之Spinner的简单探讨

    一.前言 今天用了一下Spinner这个控件,主要是结合官网的例子来用的,不过官网的是把数据写在Strings.xml中的, 某种程度上,不是很符合我们需要的,比较多的应该都是从数据库读出来,绑定上去 ...

  3. Xamarin.Android之UI Test简单入门

    一.前言 相信Xamarin免费之后会有更多的人加入进来,这也是我一直以来最希望看到的事,更多的人加入到这个社区中,为这个社区贡献自己的一份力量,国内当前还没有一个比较正规或者说是名气比较大的Xama ...

  4. Android调用Jni,非常简单的一个Demo

    step1:创建一个android项目       Project Name:jnitest       Build Target: Android 1.6       Application Nam ...

  5. flask框架(二):简单的登录demo

    一:main.py # -*- coding: utf-8 -*- # @Author : Felix Wang # @time : 2018/7/3 22:58 from flask import ...

  6. Xamarin.Android之简单的抽屉布局

    0x01 前言 相信对于用过Android版QQ的,应该都不会陌生它那个向右滑动的菜单(虽说我用的是Lumia) 今天就用Xamarin.Android实现个比较简单的抽屉布局.下面直接进正题. 0x ...

  7. Xamarin Android自定义文本框

    xamarin android 自定义文本框简单的用法 关键点在于,监听EditText的内容变化,不同于java中文本内容变化去调用EditText.addTextChangedListener(m ...

  8. MVP架构在xamarin android中的简单使用

    好几个月没写文章了,使用xamarin android也快接近两年,还有一个月职业生涯就到两个年了,从刚出来啥也不会了,到现在回头看这个项目,真jb操蛋(真辛苦了实施的人了,无数次吐槽怎么这么丑),怪 ...

  9. 基于Xamarin Android实现的简单的浏览器

    最近做了一个Android浏览器,当然功能比较简单,主要实现了自己想要的一些功能……现在有好多浏览器为什么还要自己写?当你使用的时候总有那么一些地方不如意,于是就想自己写一个. 开发环境:Xamari ...

随机推荐

  1. ASP.NET Core 使用 Redis 和 Protobuf 进行 Session 缓存

    前言 上篇博文介绍了怎么样在 asp.net core 中使用中间件,以及如何自定义中间件.项目中刚好也用到了Redis,所以本篇就介绍下怎么样在 asp.net core 中使用 Redis 进行资 ...

  2. Android提权漏洞CVE-2014-7920&CVE-2014-7921分析

    没羽@阿里移动安全,更多安全类技术干货,请访问阿里聚安全博客 这是Android mediaserver的提权漏洞,利用CVE-2014-7920和CVE-2014-7921实现提权,从0权限提到me ...

  3. IEEE754、VAX、IBM浮点型介绍和.NET中互相转换

    [题外话] 最近在做C3D文件的解析,好奇怪的是文件中竟然存储了CPU的类型,原本不以为然,结果后来读取一个文件发现浮点数全部读取错误.查了下发现虽然在上世纪80年代就提出了IEEE754要统一浮点数 ...

  4. 【VC++技术杂谈004】使用微软TTS语音引擎实现文本朗读

    本文主要介绍如何使用微软TTS语音引擎实现文本朗读,以及生成wav格式的声音文件. 1.语音引擎及语音库的安装 TTS(Text-To-Speech)是指文本语音的简称,即通过TTS引擎把文本转化为语 ...

  5. OracleConnection is obsolete

    用EF搞Oracle的 fake CodeFirst 时,一直报错以下错误: 对类型“System.Data.OracleClient.OracleConnection”的存储区提供程序实例调用“ge ...

  6. Atitit.创业之uke团队规划策划 v9

    Atitit.创业之uke团队规划策划 v9 Uke  org prjAuthor撰写人:绰号:老哇的爪子( 全名::Attilax Akbar Al Rapanui 阿提拉克斯 阿克巴 阿尔 拉帕努 ...

  7. kafka 安装出现的几个问题

    1.安装kafka的过程出现两个问题 1)错误: 找不到或无法加载主类 kafka.Kafka 原因:    下载的是源码包,需要编译.可以下载Binary downloads: 2) ERROR I ...

  8. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  9. Latch1:理解 PageIOLatch和PageLatch

    Latch主要分为三种,Buffer Latch,I/O Latch, non-buf latch. 1,PageLatch 在访问数据库的数据页(Data Page或Index Page)时,如果相 ...

  10. VS2013常用快捷键你敢不会?

    F1 帮助文档 F5 运行 F12 跳转到定义 F11 单步调试 Shift+F5 停止调试 Ctrl+滚轮 放大缩小当前视图 Ctrl+L 删除当前行 Ctrl+K,Ctrl+C 注释选中代码 Ct ...