今天了解了一下androidclient与服务端是如何交互的,发现事实上跟web有点类似吧,然后网上找了大神的登陆演示样例。是基于IntentService的
1.后台使用简单的servlet,支持GET或POST。
这个servlet终于返回给前台一个字符串flag,值是true或false,表示登录是否成功。
servlet使用之前须要配置。主义servlet的servlet-name要和servlet-mapping的servlet-name一致。否则找不到路径
我是在myEclipse上创建的一个web service 项目。然后部署到tomcatserver上以便androidclient訪问
helloWorld com.zhongzhong.wap.action.HelloServlet helloWorld /queryOrder
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,分别作为登录界面和登录成功界面。
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 server响应字符串 * @throws Exception */ public static String getRequest(String url) throws Exception { // 创建HttpGet对象。 HttpGet get = new HttpGet(url); // 发送GET请求 HttpResponse httpResponse = httpClient.execute(get); // 假设server成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取server响应字符串 String result = EntityUtils.toString(httpResponse.getEntity()); return result; } else { Log.d("server响应代码", (new Integer(httpResponse.getStatusLine() .getStatusCode())).toString()); return null; } } /** * * @param url * 发送请求的URL * @param params * 请求參数 * @return server响应字符串 * @throws Exception */ public static String postRequest(String url, MaprawParams) 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); // 假设server成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取server响应字符串 String result = EntityUtils.toString(httpResponse.getEntity()); return result; } return null; } }
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获取主线程传来的username与password字符串 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封装请求參数 HashMap5。map = new HashMap (); 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("server返回值", strFlag); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(strFlag.trim().equals("true")){ return true; }else{ return false; } } }
在AndroidManifest.xml中注冊IntentService。
注意uses-permission节点。为程序开启訪问网络的权限。
6.登陆界面处理,注意
- button监听事件中。使用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; }}
执行截图:
转载自