1. 程式人生 > >HTTP伺服器與android客戶端的json互動

HTTP伺服器與android客戶端的json互動


廢話就不多說了,直接上程式碼:

伺服器端:

①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;
	}
}
⑤在MainActivity中實現登入操作:
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);
		}

	}
}
具體的其它相關類請下載下面的原始碼檢視。記住一定不要忘了在AndroidManifest.xml檔案中新增訪問網路的許可權:
<uses-permission android:name="android.permission.INTERNET" />

好了,自己寫寫體驗一下吧。程式碼不太符合架構規範,旨在展示Android與Web伺服器的Json資料互動過程。寫的不好還望各位大神指教。

原始碼免費下載:

原始碼免費下載