版權聲明:如需轉載,請標明出處:http://blog.csdn.net/shangmingchao【商明超的博客】
廢話就不多說了,直接上代碼:
服務器端:
①MySQL中建立一個數據庫hello,建一張表tab_user,添加字段id,username,password。然後隨便添加幾條記錄。
②新建Java Web工程HelloServer。
③在WEB-INF目錄下的lib中引入mysql-connector-java-5.0.8-bin.jar與org.json.jar。
④新建Servlet類LoginServlet。
LoginServlet.java:
- package cn.domain.hello.servlet;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import cn.domain.hello.bean.UserBean;
- import cn.domain.hello.dao.UserDao;
- public class LoginServlet extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request
- * the request send by the client to the server
- * @param response
- * the response send by the server to the client
- * @throws ServletException
- * if an error occurred
- * @throws IOException
- * if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to
- * post.
- *
- * @param request
- * the request send by the client to the server
- * @param response
- * the response send by the server to the client
- * @throws ServletException
- * if an error occurred
- * @throws IOException
- * if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/json;charset=UTF-8");
- String reqMessage, respMessage;
- JSONArray reqObject = null;
- JSONArray respObject = null;
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- request.getInputStream(), "UTF-8"));
- StringBuffer sb = new StringBuffer("");
- String temp;
- while ((temp = br.readLine()) != null) {
- sb.append(temp);
- }
- br.close();
- reqMessage = sb.toString();
- System.out.println("請求報文:" + reqMessage);
- reqObject = new JSONArray(reqMessage);
- UserDao userDao = new UserDao();
- UserBean ub = userDao.getUserByName(reqObject.getJSONObject(0)
- .getString("username"));
- if (ub.getPassword() != null
- && ub.getPassword().equals(
- reqObject.getJSONObject(0).getString("password"))) {
- respObject = new JSONArray().put(new JSONObject().put("userId",
- ub.getId()));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- respMessage = respObject == null ? "" : respObject.toString();
- System.out.println("返回報文:" + respMessage);
- PrintWriter pw = response.getWriter();
- pw.write(respMessage);
- pw.flush();
- pw.close();
- }
- }
- }
簡單抽象出了數據訪問層,並實現數據庫的操作,目錄結構如下,由於文件多,詳細請下載下面的源碼查看:
好了,到這裏服務器端就已經建立完成了。客戶端訪問這個servlet的URL爲:http://10.0.2.2:8080/HelloServer/servlet/LoginServlet。
記住,在Android中localhost指的是模擬器本身,而這裏的10.0.2.2指你的計算機服務器本地測試IP,可改成內網或外網IP。
Android客戶端端:
①新建一個android application:Hello。
②引入org.json.jar。
③在activity_main.xml佈局文件中加入兩個EditText,來輸入用戶名和密碼,添加按鈕來提交json數據。
④先封裝一下訪問網絡的HTTP操作:
WebUtil.java:
- package cn.domain.hello.util;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONArray;
- import cn.domain.hello.config.Config;
- public class WebUtil {
- public static JSONArray getJSONArrayByWeb(String methodName,
- JSONArray params) {
- String returnValue = "";
- JSONArray result = null;
- HttpParams httpParams = new BasicHttpParams();
- httpParams.setParameter("charset", "UTF-8");
- HttpClient hc = new DefaultHttpClient(httpParams);
- HttpPost hp = new HttpPost(Config.SERVER_IP + "/HelloServer/servlet/"
- + methodName);
- try {
- hp.setEntity(new StringEntity(params.toString(), "UTF-8"));
- HttpResponse hr = hc.execute(hp);
- if (hr.getStatusLine().getStatusCode() == 200) {
- returnValue = EntityUtils.toString(hr.getEntity(), "UTF-8");
- result = new JSONArray(returnValue);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if (hc != null) {
- hc.getConnectionManager().shutdown();
- }
- return result;
- }
- }
- package cn.domain.hello.activity;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import cn.domain.hello.R;
- import cn.domain.hello.config.Config;
- import cn.domain.hello.util.WebUtil;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewStub;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText etUsername;
- private EditText etPassword;
- private Button btnLogin;
- private ViewGroup vsProgress;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.etUsername = (EditText) this.findViewById(R.id.etUsername);
- this.etPassword = (EditText) this.findViewById(R.id.etPassword);
- this.btnLogin = (Button) this.findViewById(R.id.btnLogin);
- this.btnLogin.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String username = MainActivity.this.etUsername.getText()
- .toString().trim();
- String password = MainActivity.this.etPassword.getText()
- .toString().trim();
- if ("".equals(username)) {
- Toast.makeText(MainActivity.this, "請填寫用戶名",
- Toast.LENGTH_SHORT).show();
- return;
- }
- if ("".equals(password)) {
- Toast.makeText(MainActivity.this, "請填寫密碼",
- Toast.LENGTH_SHORT).show();
- return;
- }
- //如果已經填寫了用戶名和密碼,執行登錄操作
- executeLogin(username, password);
- }
- });
- }
- private void executeLogin(String username, String password) {
- new LoginTask().execute(username, password);
- }
- private void onLoginComplete(Integer userId) {
- if (userId == null || userId == 0) {//如果沒有獲取到用戶ID,說明登錄失敗
- Toast.makeText(MainActivity.this, "用戶名或密碼錯誤", Toast.LENGTH_SHORT)
- .show();
- if (vsProgress != null) {
- vsProgress.setVisibility(View.INVISIBLE);
- }
- return;
- }
- if (vsProgress != null) {
- vsProgress.setVisibility(View.INVISIBLE);
- }
- //如果成功獲取到返回的用戶ID,說明登錄成功,跳轉到HelloActivity
- Toast.makeText(MainActivity.this, "登陸成功", Toast.LENGTH_SHORT).show();
- HelloActivity.actionStart(MainActivity.this, userId, etUsername
- .getText().toString());
- }
- private class LoginTask extends AsyncTask<String, Void, Integer> {
- @Override
- protected void onPreExecute() {
- // TODO Auto-generated method stub
- super.onPreExecute();
- //進行登錄驗證時,顯示登錄進度條
- if (vsProgress == null) {
- ViewStub vs = (ViewStub) findViewById(R.id.vsProgress);
- vsProgress = (ViewGroup) vs.inflate();
- } else {
- vsProgress.setVisibility(View.VISIBLE);
- }
- }
- @Override
- protected Integer doInBackground(String... params) {
- // TODO Auto-generated method stub
- Integer result = null;
- JSONArray reqValue;
- try {
- //將用戶名和密碼封裝到JSONArray中,進行HTTP通信
- reqValue = new JSONArray().put(new JSONObject().put("username",
- params[0]).put("password", params[1]));
- JSONArray rec = WebUtil.getJSONArrayByWeb(Config.METHOD_LOGIN,
- reqValue);
- if (rec != null) {//如果成功獲取用戶ID
- result = rec.getJSONObject(0).getInt("userId");
- }
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
- @Override
- protected void onPostExecute(Integer result) {
- // TODO Auto-generated method stub
- super.onPostExecute(result);
- //回調
- onLoginComplete(result);
- }
- }
- }
- <uses-permission android:name="android.permission.INTERNET" />
好了,自己寫寫體驗一下吧。代碼不太符合架構規範,旨在展示Android與Web服務器的Json數據交互過程。寫的不好還望各位大神指教。
源碼免費下載:
源碼下載