開發日誌—詳細的連結的伺服器代碼(登入),日誌連結
進行連結的伺服器 ,輸入密碼帳號,登入頁面 是基本APP都有的功能,總結了下:過
使用者登陸介面
/** * @author think *使用者登陸介面 */public class LoginUi extends Activity implements HttpCallBack{private EditText emailET;private EditText passwordET;private ImageButton loginIB;private TextView registerTV;private TextView pwdForgetTV;private User user;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_login);initViews();setListener();}<span style="background-color: rgb(255, 204, 255);">private void setListener() {loginIB.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {UserLogin userlogin=null;try {userlogin = (UserLogin) BeanFactory.getInstance("UserLogin");} catch (Exception e) {BasicUtil.showMassage(LoginUi.this, "sorry, class not found");}getUserData();if(user==null){return;}userlogin.loginResult(user,LoginUi.this);}});</span>registerTV.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {startActivity(RegisterUi.class);}});pwdForgetTV.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {startActivity(FindPasswordUi.class);}});}/** * 初始化組件 */private void initViews() {emailET=(EditText)findViewById(R.id.et_login_email);passwordET=(EditText)findViewById(R.id.et_login_password);loginIB = (ImageButton)findViewById(R.id.ib_login_submit);registerTV = (TextView)findViewById(R.id.tv_login_register);pwdForgetTV=(TextView) findViewById(R.id.tv_login_forgetpassord);// TODO 初始化組件}/** * 獲得使用者的登入資料 */private void getUserData() {String username=emailET.getText().toString();String password=passwordET.getText().toString();if(TextUtils.isEmpty(username)){BasicUtil.showMassage(this, "郵箱不可為空");return;}if(TextUtils.isEmpty(password)){BasicUtil.showMassage(this, "密碼不可為空");return;}user=new User(username, password);}private void startActivity(Class<?> clazz){Intent intent=new Intent(this,clazz);startActivity(intent);}@Overridepublic boolean onMenuItemSelected(int featureId, MenuItem item) {return super.onMenuItemSelected(featureId, item);}@Overrideprotected void finalize() throws Throwable {super.finalize();}@Override<span style="background-color: rgb(255, 204, 255);">public void callBack(Object object) {Message message = handler.obtainMessage();message.what = 1;message.obj = object;handler.sendMessage(message);}Handler handler=new Handler(){@Overridepublic void handleMessage(android.os.Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 1: System.out.println("結果:"+ msg.obj);//這裡就是你得到的結果 可以再這裡修改UI if((Boolean)msg.obj==true){startActivity(MainUi.class);}else{BasicUtil.showMassage(LoginUi.this, "登入失敗");} break;default:break;}}};</span>}
回呼函數介面HttpCallBack
public interface HttpCallBack {void callBack(Object object);}
使用者登入業務類UserLogin
/** * @author think 使用者登入業務類 */public class UserLogin {/** * 使用者登入業務 * * @param userinfo * @return 登入結果 */public void loginResult(User userinfo, HttpCallBack httpCallBack) {ArrayList<NameValuePair> pairs = getPairs(userinfo);String uri = "http://" + ConstValue.IP + ":" + ConstValue.PORT;try {ApacheHttpClient.httpPost(uri, pairs, httpCallBack);} catch (Exception e) {e.printStackTrace();}}/** * @param userinfo * 封裝使用者登入資訊 */private ArrayList<NameValuePair> getPairs(User userinfo) {ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();NameValuePair pair = null;pair = new BasicNameValuePair("username", userinfo.getEmail());pairs.add(pair);pair = new BasicNameValuePair("password", userinfo.getPassword());pairs.add(pair);return pairs;}}
發送Http請求ApacheHttpClient
/** * @author think 發送Http請求 */public class ApacheHttpClient {/** * @return * */public static String httpGet(String uri) {String response = null;// 響應HttpClient httpClient = new DefaultHttpClient();// 建立HttpGet對象HttpGet httpGet = new HttpGet(uri);HttpResponse httpResponse;try {// 使用execute方法發送HTTP GET請求,並返回HttpResponse對象httpResponse = httpClient.execute(httpGet);int statusCode = httpResponse.getStatusLine().getStatusCode();// 返回碼// ,if (statusCode == HttpStatus.SC_OK) {// 獲得返回結果response = EntityUtils.toString(httpResponse.getEntity());} else {response = "返回碼:" + statusCode;}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(response);return response;}/** * 以Post方式發送請求 * * @param url * 請求地址 * @param params * 參數 ,Post方式必須用NameValuePair[]陣列儲存參數 * @return * @throws Exception */<span style="background-color: rgb(255, 204, 255);">public static void httpPost(final String uri,final List<NameValuePair> params,final HttpCallBack httpCallBack) throws Exception {new Thread(new Runnable() {public void run() {String response = null;HttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = new HttpPost(uri);try {// 設定httpPost請求參數httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));// 使用execute方法發送HTTP Post請求,並返回HttpResponse對象,會有延遲等待HttpResponse httpResponse = httpClient.execute(httpPost);int statusCode = httpResponse.getStatusLine().getStatusCode();// 返回碼 ,if (statusCode == HttpStatus.SC_OK) {response = EntityUtils.toString(httpResponse.getEntity());JSONObject obj = new JSONObject(response);Boolean result = (Boolean) obj.opt("success");Log.i("----", "result=" + result);httpCallBack.callBack(result);//回調} else {response = "返回碼:" + statusCode; }} catch (Exception e) {e.printStackTrace();}}}).start();// get-->opt// ToDo:解析obj}}</span>