Google用户登录界面 Android实现
实验效果:
项目目录:
Java代码(放在Src文件下)
package com.bn.chap9.login;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Sample9_1_Activity extends Activity {
EditText user;//用户名
EditText password;//密码
Button bOK;//确定按钮
Button bClear;//清空按钮
TextView tv;
DefaultHttpClient client;
HttpPost httpPost;
HttpResponse response;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
client=new DefaultHttpClient();
httpPost=new HttpPost("https://www.google.com/accounts/ClientLogin");
user=(EditText)this.findViewById(R.id.EditText01);
password=(EditText)this.findViewById(R.id.EditText02);
bOK=(Button)this.findViewById(R.id.Button01);
bClear=(Button)this.findViewById(R.id.Button02);
tv=(TextView)this.findViewById(R.id.TextView03);
bClear.setOnClickListener
(
new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
user.setText("");
password.setText("");
}
}
);
bOK.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=user.getText().toString().trim();
System.out.println("用户姓名是:"+userName);
String pwd=password.getText().toString().trim();
System.out.println("用户密码是:"+pwd);
if(userName.length()==0)
{
Toast.makeText(
Sample9_1_Activity.this,
"请输入用户名!",
Toast.LENGTH_SHORT).show();
}else if(pwd.length()==0)
{
Toast.makeText(
Sample9_1_Activity.this,
"请输入密码!",
Toast.LENGTH_SHORT).show();
}
else
{
String result=getToken(userName,pwd);
System.out.println("返回结果是:"+result);
if(result.length()!=0)
{
Toast.makeText(
Sample9_1_Activity.this,
"恭喜您,成功获取Token!",
Toast.LENGTH_SHORT).show();
tv.setText("获取的Token为:"+result);
Header[] header=new BasicHeader[5];//创建Header对象
header[0]=new BasicHeader("Content-type","application/x-www-form-urlencoded");
header[1]=new BasicHeader("Authorzation","GoogleLogin auth=\""+result+"\"");
header[2]=new BasicHeader("User-Agent","Java/1.5.0_06");
header[3]=new BasicHeader("Accept","text/html,image/gif,image/jpeg,*;q=2,*/*;q=.2");
header[4]=new BasicHeader("Connection","keep-alive");
//用于发送HttpGet请求
String url="http://www.google.com/ig?hl=zh-CN&refresh=1";//网页地址
HttpGet get=new HttpGet(url);
for(int i=0;i<header.length;i++)
{
get.addHeader(header[i]);
}
try {
response=client.execute(get);
InputStream tempIs=response.getEntity().getContent();
BufferedReader read=new BufferedReader(new InputStreamReader(tempIs));
StringBuffer sb=new StringBuffer();
String tempStr=null;
try
{
while((tempStr=read.readLine())!=null)
{
sb.append(tempStr);
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
tempIs.close();//关闭输入流
}catch(Exception e)
{
e.printStackTrace();
}
}
tempStr=sb.toString().trim();
System.out.println("tempStr的结果:"+tempStr);
BufferedWriter bw=new BufferedWriter(new FileWriter("/sdcard/user.txt"));
bw.write(tempStr,0,tempStr.length());
bw.flush();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else
{
Toast.makeText(
Sample9_1_Activity.this,
"很遗憾,未能获取Token,请检查账号和密码是否正确!",
Toast.LENGTH_SHORT).show();
tv.setText("未能获取Token!");
}
}
}
}
);
}
public String getToken(String name,String pwd)
{
String result=null;
List<NameValuePair> list=new ArrayList<NameValuePair>();//创建NameValurPair字符串
list.add(new BasicNameValuePair("Email",name));//添加账号
list.add(new BasicNameValuePair("Passwd",pwd));//添加密码
list.add(new BasicNameValuePair("source","clientstr"));
list.add(new BasicNameValuePair("service","reader"));
try
{
httpPost.setEntity(
new UrlEncodedFormEntity(
list,
HTTP.DEFAULT_CONTENT_CHARSET));
response=client.execute(httpPost);
if(response.getStatusLine().getStatusCode()!=200)
{
return "";
}
InputStream is=response.getEntity().getContent();
result=getAuth(is);//获取信息
}catch(Exception e)
{
e.printStackTrace();
}
return result;
}
public String getAuth(InputStream is)
{//分离Token的方法
String result=null;
String line=null;
BufferedReader read=new BufferedReader(
new InputStreamReader(is));//创建BufferedReader对象
try
{
while((line=read.readLine())!=null)//读取信息
{
if(line.startsWith("Auth="))
{
result=line.substring(5);//截取字符串
}
}
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
is.close();//关闭输入流
}catch(Exception e)
{
e.printStackTrace();
}
}
return result;
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffcc">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google用户登录系统"
android:textColor="#222222"
android:textSize="18dip"
/>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffcc66">
<TextView
android:text="用户名:"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#222222"
android:textSize="18dip">
</TextView>
<EditText
android:text=""
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dip">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffcc66">
<TextView
android:text="密 码:"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#222222"
android:textSize="18dip">
</TextView>
<EditText
android:text=""
android:id="@+id/EditText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dip"
android:password="true">
</EditText>
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffcc66">
<Button
android:text="登录"
android:id="@+id/Button01"
android:layout_width="75dip"
android:layout_height="40dip"
android:textSize="18dip"
android:gravity="center">
</Button>
<Button
android:text="清空"
android:id="@+id/Button02"
android:layout_width="75dip"
android:layout_height="40dip"
android:textSize="18dip"
android:gravity="center">
</Button>
</LinearLayout>
<TextView
android:text=""
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#222222"
android:textSize="18dip">
</TextView>
</LinearLayout>
Google用户登录界面 Android实现的更多相关文章
- Android studio 开发一个用户登录界面
Android studio 开发一个用户登录界面 activity_main.xml <?xml version="1.0" encoding="utf-8&qu ...
- jQuery和CSS3炫酷GOOGLE样式的用户登录界面
这是一款使用jQuery和CSS3打造的GOOGLE样式的用户登录界面特效.该登录界面特效中,右上角的小问号和错误提示小图标使用SVG来制作.username和password输入框採用浮动标签特效. ...
- 很漂亮的用户登录界面HTML模板
效果预览:http://keleyi.com/keleyi/phtml/divcss/21.htm HoverTree开源项目实现了分层后,准备实现管理员后台登录,这里先把登录界面的HTML模板整理好 ...
- 美化VC界面(用户登录界面)
源代码:下载 VC开发程序单调的界面相信大家都是深有感触,提到界面美化编程,人们都会说做界面不要用VC写,太难了.一句俗语:难者不会,会者不难.VC的美化界面编程并没有人们想像的那么难.这篇文章是我写 ...
- html简约风用户登录界面网页制作html5-css-jquary-学习模版
2018--12-12 喜迎双十二,咳咳,,,,我不是打广告哈,购物的节日也不要忘记学习. 大家好,我又来了. 今天抽出来空把自己的学习心得给大家分享,这是一个可开发可扩展的用户登录界面,用于开发学习 ...
- 编写Java程序,使用Swing布局管理器与常用控件,实现用户登录界面
返回本章节 返回作业目录 需求说明: 使用Swing布局管理器与常用控件,实现用户登录界面 实现思路: 创建用户登录界面的类LoginFrame,在该类中创建无参数的构造方法,在构造方法中,设置窗体大 ...
- java web用户登录界面
做这次实验,主要用到了mysql java web 的 内容 实验代码: IUserDao.java package com.jaovo.msg.dao; import java.util.List ...
- Qt 用户登录界面
使用QT创建自己的登录窗口: 主要步骤: 1.窗口界面的绘制 2.沟通数据库进行密码验证 void MainWindow::on_pushButton_clicked() { // 连 ...
- 记一次自启动的docker容器将宿主机的开机用户登录界面覆盖事件
宿主机的系统为CentOS7_7.7.1908,默认为GUI启动,安装了宝塔面板,docker-ce为最新版. 在启动了一个centos7的容器(镜像为centos官方镜像)后,将该容器重启策略设置为 ...
随机推荐
- uboot: 理解uboot要看哪些书
概览:
- 配置greenplum参数
在进行一个greenplum安装之前需要进行配置一下相关的系统参数,否则很容易出现意想不到的错误. 1.修改系统参数 编辑 /etc/sysctl.conf ,以下是最小配置 kernel.shmma ...
- Eclipse使用技巧总结(一)
一.建立工作空间 如上图所示,可以建立新的工作空间,或者切换工作空间. 二.导入导出工作空间配置 三.设置行号 如图,用鼠标在坐变阴影部分右击弹出菜单,选中Show Line Numbers一项. 四 ...
- 深入理解-HashMap
一.HashMap概述 HashMap 在家族中位置:实现了Map接口,继承AbstractMap类.HashMap 允许key/value 都为null. 二.HashMap存储结构 HashMap ...
- HDU2138 随机素数测试 Miller-Rabin算法
题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In eac ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- 由动态库文件dll生成lib库文件(手动生成.def文件,然后使用lib命令编译,非常牛),同理可使用dll生成.a库文件
本文基于OpenBlas的编译和安装,来说明如何从一个dll文件生成lib库文件. 参考OpenBlas的说明“Howto generate import library for MingW”,和Mi ...
- VM添加e1000e驱动网卡
关闭虚拟机 打开VMware 虚拟机配置 (.vmx),如
- 应用程序无法正常启动0xc000007b
参考: http://jingyan.baidu.com/article/ff42efa9181bbbc19e22022f.html DirectX修复工具: http://blog.csdn.net ...
- WebView.destroy() called while still attached 的解决的方法
能够如今webView的父组件中删除该webview,然后再Destroy parent.removeView(webView); 然后 webView.removeAllViews(); webVi ...