Google用户登录界面 Android实现

  1. 云栖社区>
  2. 博客>
  3. 正文

Google用户登录界面 Android实现

聚优云惠 2013-09-11 12:40:19 浏览219
展开阅读全文
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qingfeng812/article/details/11559655

实验效果:

项目目录:

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>

网友评论

登录后评论
0/500
评论
聚优云惠
+ 关注