Android客户端与服务端交互之登陆示例
- Android客户端与服务端交互之登陆示例
-
今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的
1.后台使用简单的servlet,支持GET或POST。这个servlet最终返回给前台一个字符串flag,值是true或false,表示登录是否成功。
servlet使用之前需要配置,主义servlet的servlet-name要和servlet-mapping的servlet-name一致,否则找不到路径
我是在myEclipse上创建的一个web service 项目,然后部署到tomcat服务器上以便android客户端访问
<servlet>
<servlet-name>helloWorld</servlet-name>
<servlet-class>com.zhongzhong.wap.action.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloWorld</servlet-name>
<url-pattern>/queryOrder</url-pattern>
</servlet-mapping>import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import com.zhongzhong.wap.bean.UserBean; public class HelloServlet extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { resp.setContentType(text/html);
PrintWriter out = resp.getWriter();
Boolean flag = false;
String userName = req.getParameter(un);
String password = req.getParameter(pw);
if(userName.equals(htp)&&password.equals(123))
{
flag = true;
} else flag = false;
System.out.println(userName:+userName+ password:+password);
out.print(flag);
out.flush();
out.close();
} }2.然后我是在安卓的ADT上创建一个安卓项目,建立两个Activity,分别作为登录界面和登录成功界面。
1 <relativelayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
2
3 <textview android:id="@+id/textView1"
android:layout_alignparenttop="true"
android:layout_centerhorizontal="true"
android:layout_height="wrap_content"
android:layout_margintop="40dp"
android:layout_width="wrap_content"android:text="HelloWorld登陆示例"> <edittext
android:ems="10"
android:hint="请输入账号"
android:id="@+id/et_user"
android:layout_below="@+id/textView1"
android:layout_centerhorizontal="true"
android:layout_height="wrap_content"
android:layout_margintop="33dp"
android:layout_width="wrap_content"> <requestfocus>
</requestfocus>
</edittext> <edittext
android:ems="10"
android:hint="请输入密码"
android:id="@+id/et_psw"
android:inputtype="textPassword"
android:layout_below="@+id/et_user"
android:layout_centerhorizontal="true"
android:layout_height="wrap_content"
android:layout_margintop="40dp"
android:layout_width="wrap_content"> <button
android:id="@+id/btn_login"
android:layout_below="@+id/et_psw"
android:layout_centerhorizontal="true"
android:layout_height="wrap_content"
android:layout_margintop="37dp"
android:layout_width="wrap_content"
android:text="登陆">
</button>
</edittext>
</textview>
</relativelayout> -
<relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".NaviActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <textview android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_height="wrap_content" android:layout_margintop="46dp" android:layout_width="wrap_content" android:text="登陆成功"> </textview></relativelayout>3.HTTP的访问公共类,用于处理GET和POST请求
package com.example.logindemo; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import android.content.Entity;
import android.util.Log; public class HttpUtil {
// 创建HttpClient对象
public static HttpClient httpClient = new DefaultHttpClient();
public static final String BASE_URL = http://192.168.3.14:8090/HelloWord/; /**
*
* @param url
* 发送请求的URL
* @return 服务器响应字符串
* @throws Exception
*/
public static String getRequest(String url) throws Exception {
// 创建HttpGet对象。
HttpGet get = new HttpGet(url);
// 发送GET请求
HttpResponse httpResponse = httpClient.execute(get);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
} else {
Log.d(服务器响应代码, (new Integer(httpResponse.getStatusLine()
.getStatusCode())).toString());
return null;
}
} /**
*
* @param url
* 发送请求的URL
* @param params
* 请求参数
* @return 服务器响应字符串
* @throws Exception
*/
public static String postRequest(String url, Map<string, string=""> rawParams)
throws Exception {
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<namevaluepair> params = new ArrayList<namevaluepair>();
for (String key : rawParams.keySet()) {
// 封装请求参数
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(params, UTF-8));
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
return null;
}
}
</namevaluepair></namevaluepair></string,>4.IntentService服务,用于在后台以队列方式处理耗时操作。
package com.example.logindemo; import java.util.HashMap; import android.app.IntentService;
import android.content.Intent;
import android.util.Log; public class ConnectService extends IntentService {
private static final String ACTION_RECV_MSG = com.example.logindemo.action.RECEIVE_MESSAGE; public ConnectService() {
super(TestIntentService);
// TODO Auto-generated constructor stub
} @Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
/**
* 经测试,IntentService里面是可以进行耗时的操作的
* IntentService使用队列的方式将请求的Intent加入队列,
* 然后开启一个worker thread(线程)来处理队列中的Intent
* 对于异步的startService请求,IntentService会处理完成一个之后再处理第二个
*/
Boolean flag = false;
//通过intent获取主线程传来的用户名和密码字符串
String username = intent.getStringExtra(username);
String password = intent.getStringExtra(password);
flag = doLogin(username, password);
Log.d(登录结果, flag.toString()); Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ACTION_RECV_MSG);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(result, flag.toString());
sendBroadcast(broadcastIntent); } // 定义发送请求的方法
private Boolean doLogin(String username, String password)
{
String strFlag = ;
// 使用Map封装请求参数
HashMap<string, string=""> map = new HashMap<string, string="">();
map.put(un, username);
map.put(pw, password);
// 定义发送请求的URL
String url = HttpUtil.BASE_URL + queryOrder?un= + username + &pw= + password; //GET方式
// String url = HttpUtil.BASE_URL + LoginServlet; //POST方式
Log.d(url, url);
Log.d(username, username);
Log.d(password, password);
try {
// 发送请求
strFlag = HttpUtil.postRequest(url, map); //POST方式
// strFlag = HttpUtil.getRequest(url); //GET方式
Log.d(服务器返回值, strFlag);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if(strFlag.trim().equals(true)){
return true;
}else{
return false;
} }
}
</string,></string,>5。在AndroidManifest.xml中注册IntentService。注意uses-permission节点,为程序开启访问网络的权限。
<!--?xml version=1.0 encoding=utf-8?-->
<manifest android:versioncode="1" android:versionname="1.0" package="com.example.logindemo" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk android:minsdkversion="8" android:targetsdkversion="18"> <uses-permission android:name="android.permission.INTERNET"> <intent-filter> <category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity> </activity> <service android:name="com.example.logindemo.ConnectService">
</service>
</application> </uses-permission></uses-sdk></manifest>6.登陆界面处理,注意
- 按钮监听事件中,使用Intent将要传递的值传给service。接收广播类中,同样使用Intent将要传递的值传给下一个Activity。在onCreate()中,动态注册接收广播类的实例receiver。在接收广播类中,不要使用完毕后忘记注销接收器,否则会报一个Are you missing a call to unregisterReceiver()? 的异常。
package com.example.logindemo; import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private static final String ACTION_RECV_MSG = com.example.logindemo.action.RECEIVE_MESSAGE;
private Button loginBtn;
private EditText et_username;
private EditText et_password;
private String userName;
private String passWord;
private MessageReceiver receiver ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//动态注册receiver
IntentFilter filter = new IntentFilter(ACTION_RECV_MSG);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MessageReceiver();
registerReceiver(receiver, filter);
} private void initView() {
// TODO Auto-generated method stub
et_username = (EditText)findViewById(R.id.et_user);
et_password =( EditText)findViewById(R.id.et_psw);
loginBtn = (Button)findViewById(R.id.btn_login);
loginBtn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(matchLoginMsg())
{
// 如果校验成功
Intent msgIntent = new Intent(MainActivity.this, ConnectService.class);
msgIntent.putExtra(username, et_username.getText().toString().trim());
msgIntent.putExtra(password, et_password.getText().toString().trim());
startService(msgIntent);
} }
});
} protected boolean matchLoginMsg() {
// TODO Auto-generated method stub
userName = et_username.getText().toString().trim();
passWord = et_password.getText().toString().trim();
if(userName.equals())
{
Toast.makeText(MainActivity.this, 账号不能为空,Toast.LENGTH_SHORT).show();
return false;
}
if(passWord.equals())
{
Toast.makeText(MainActivity.this, 密码不能为空,Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
//接收广播类
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra(result);
Log.i(MessageReceiver, message);
// 如果登录成功
if (message.equals(true)){
// 启动Main Activity
Intent nextIntent = new Intent(MainActivity.this, NaviActivity.class);
startActivity(nextIntent);
// 结束该Activity
finish();
//注销广播接收器
context.unregisterReceiver(this);
}else{
Toast.makeText(MainActivity.this, 用户名或密码错误,请重新输入!,Toast.LENGTH_SHORT).show();
} }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }运行截图:


- 按钮监听事件中,使用Intent将要传递的值传给service。接收广播类中,同样使用Intent将要传递的值传给下一个Activity。在onCreate()中,动态注册接收广播类的实例receiver。在接收广播类中,不要使用完毕后忘记注销接收器,否则会报一个Are you missing a call to unregisterReceiver()? 的异常。
Android客户端与服务端交互之登陆示例的更多相关文章
- java客户端与服务端交互通用处理 框架解析
一.综述 java 客户端与服务端交互过程中,采用NIO通讯是异步的,客户端基本采用同一处理范式,来进行同异步的调用处理. 处理模型有以下几个要素: 1. NIO发送消息后返回的Future 2. 每 ...
- c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP 入门级客户端与服务端交互代码 网 ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(3) 前后台打通
4.2.1.2.4 PipelineDraweeControllerBuilder.obtainController()源码分析 续 上节中我们提到两个核心的步骤 obtainDataSourceSu ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
4.2 Fresco客户端与服务端的交互(一) 解决Q1问题 从这篇博客开始,我们开始讨论客户端与服务端是如何交互的,这个交互的入口,我们从Q1问题入手(博客按照这样的问题入手,是因为当时我也是从这里 ...
- socket 通信 入门3 android 客户端 C# 服务端
这是一个android端操控服务器的例子 就是发送简单指令到服务器 然后服务器响应什么的... 当然这里是未完成的 只是简单展示一下大致思路 首先连接建立起来后 服务端给客户端一条信息 告诉 ...
- android 38 Abdroid客户端和服务端交互
服务端: package com.sxt.day05; import java.io.IOException; import java.util.ArrayList; import javax.ser ...
- Androidclient与服务端交互之登陆演示样例
今天了解了一下androidclient与服务端是如何交互的,发现事实上跟web有点类似吧,然后网上找了大神的登陆演示样例.是基于IntentService的 1.后台使用简单的servlet,支持G ...
- 转-Android客户端和服务端如何使用Token和Session
http://www.software8.co/wzjs/yidongkaifa/6407.html 对于初学者来说,对Token和Session的使用难免会限于困境,开发过程中知道有这个东西,但却不 ...
- UDP网络程序,客户端和服务端交互原理
创建一个udp客户端程序的流程是简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 UDP是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实 ...
随机推荐
- 跨平台的CStdString类,实现了CString的接口
在实际工作中,std的string功能相对于MFC的CString来说,实在是相形见绌. CStdString类实现了CString的功能,支持跨平台. // ==================== ...
- 转:基于ASP.NET的Comet长连接技术解析
原文来自于: Comet技术原理 来自维基百科:Comet是一种用于web的技术,能使服务器能实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流. 简单的 ...
- [BZOJ 1816] [Cqoi2010] 扑克牌 【二分答案】
题目链接:BZOJ - 1816 题目分析 答案具有可以二分的性质,所以可以二分答案. 验证一个答案 x 是否可行,就累加一下各种牌相对于 x 还缺少的量,如果总和超过了 x 或 m ,就不可行. 因 ...
- mouseenter 和 mouseleave
做一个下拉菜单的时候,HTML结构如下: <ul> <li class="red"> <a href="">第一级</ ...
- Java中final、finally和finalize的区别(转)
http://www.cnblogs.com/bluestorm/archive/2012/04/22/2464746.html final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖, ...
- MYSQL的硬盘IO过高引起的CPU过高判断
其实,为客户提供相关日志,不就是RACKSPACE主要作的事? 俺们以后也可以效仿的.不要去解决,而是协助客户定位. http://blog.const.net.cn/a/17275.htm 上文的思 ...
- bzoj3275
容易想到是最小割(最大权独立集)然后每个数拆成两个点,不能同时选的之间连边跑最小割,最后答案=总数-mincut/2 因为这样建图将流量变成了原来的两倍 当然这道题更好的建图方法是分成奇数和偶数两个集 ...
- Linux kernel ‘net/key/af_key.c’信息泄露漏洞
漏洞名称: Linux kernel ‘net/key/af_key.c’信息泄露漏洞 CNNVD编号: CNNVD-201307-071 发布时间: 2013-07-05 更新时间: 2013-07 ...
- Linux Shell编程(6)——变量替换
变量的名字是它的值保存的地方.引用它的值称为变量替换.$让我们仔细地区别变量和变量的值.如果variable1是一个变量的名字,那么$variable1就是引用这个变量的值――即这个变量它包含的数据. ...
- 数学(GCD,计数原理)HDU 5656 CA Loves GCD
CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...