欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能

 更新時(shí)間:2017年01月20日 15:57:09   作者:kang_ya_ping  
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android客戶(hù)端和PHP、MySQL搭建的服務(wù)器之間的簡(jiǎn)單交互,實(shí)現(xiàn)登錄功能 。

實(shí)現(xiàn)原理圖:

Handler消息機(jī)制原理:

Handler機(jī)制主要包括4個(gè)關(guān)鍵對(duì)象,分別是Message、Handler、MessageQueue、Looper。

下面對(duì)這4個(gè)關(guān)鍵對(duì)象進(jìn)行簡(jiǎn)單的介紹。

1.Message

Message是線(xiàn)程之間傳遞的消息,它可以在內(nèi)部攜帶少量的信息,用于在不同線(xiàn)程之間交換數(shù)據(jù)。Message的what字段可以用來(lái)攜帶一些整型數(shù)據(jù),obj字段可以攜帶一個(gè)Object對(duì)象。

2.Handler

Handler就是處理者的意思,它主要用于發(fā)送消息和處理消息。一般使用Handler對(duì)象的sendMessage()方法發(fā)送消息,發(fā)出的消息經(jīng)過(guò)一系列的輾轉(zhuǎn)處理后,最終會(huì)傳遞到Handler對(duì)象的HandlerMessage()方法中。

3.MessageQueue

MessageQueue是消息隊(duì)列的意思,它主要用來(lái)存放通過(guò)Handler發(fā)送的消息。通過(guò)Handler發(fā)送的消息會(huì)存在MessageQueue中等待處理。每個(gè)線(xiàn)程中只會(huì)有一個(gè)MessageQueue對(duì)象。

4.Looper

Looper是每個(gè)線(xiàn)程中的MessageQueue的管家。調(diào)用Looper的loop()方法后,就會(huì)進(jìn)入一個(gè)無(wú)限循環(huán)中。然后,每當(dāng)發(fā)現(xiàn)MessageQueue中存在一條消息,就會(huì)將它取出,并傳遞到Handler的HandlerMessage()方法中。此外,每個(gè)線(xiàn)程也只會(huì)有一個(gè)Looper對(duì)象。在主線(xiàn)程中創(chuàng)建Handler對(duì)象時(shí),系統(tǒng)已經(jīng)創(chuàng)建了Looper鍍鋅,所以不用手動(dòng)創(chuàng)建Looper對(duì)象,而在子線(xiàn)程中的Handler對(duì)象,需要調(diào)用Looper.loop()方法開(kāi)啟消息循環(huán)。

圖中可以清晰的看到整個(gè)Handler消息機(jī)制處理流程。Handler消息處理首先需要在UI線(xiàn)程創(chuàng)建一個(gè)Handler對(duì)象,然后在子線(xiàn)程中調(diào)用了sendMessage()方法,接著這個(gè)消息會(huì)存放在UI線(xiàn)程的MessageQueue中,通過(guò)Looper對(duì)象取出MessageQueue中的消息,最后分發(fā)回Handler的HandMessage()方法中。

下面是我寫(xiě)的代碼以及本程序的logcat的運(yùn)行結(jié)果。

本次任務(wù)中遇到的難題:

 無(wú)法連接到PHP服務(wù)器。實(shí)現(xiàn)了把自己輸入的用戶(hù)名和密碼封裝成了JSON但是無(wú)法發(fā)送到PHP進(jìn)行與數(shù)  據(jù)庫(kù)的比對(duì)。

package com.itcast.test03;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
 private EditText et_username;
 private EditText et_userPsd;
 private Button login;
 private Button signUp;
 private CheckBox save;
 private String user,pass;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_main);
  et_username = (EditText)findViewById(R.id.et_number);
  et_userPsd = (EditText)findViewById(R.id.et_password);
  login=(Button)findViewById(R.id.login);
  signUp=(Button)findViewById(R.id.signUp);
  save = (CheckBox)findViewById(R.id.save);  
  //登錄按鈕的點(diǎn)擊事件
  login.setOnClickListener(new View.OnClickListener() {
   
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    
     //將輸入的用戶(hù)名和密碼轉(zhuǎn)換成字符串
    String name =et_username.getText().toString();
    String pwd = et_userPsd.getText().toString();
     //調(diào)用login方法
    login(name, pwd);
    }
    });
    }
    
    private void login(final String name, final String pwd){
     //創(chuàng)建Handler對(duì)象
    final Handler handler = new Handler() {
     
    public void handleMessage(Message msg) {
    if(msg.what == 1){
     Toast.makeText(MainActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
     //提示登陸成功
     finish();
    }else if(msg.what == 0){
     Log.i(name, msg.toString());
    }else if(msg.what == -1){
     Log.i("==============",msg.toString());
    }
    }
    };
    new Thread(){
    public void run() {
     //創(chuàng)建Message對(duì)象
    Message msg =new Message();
    try {
     Post po = new Post();
     String infoo = po.logina(name, pwd);
        if(infoo != null){
        msg.what = 1;//成功
        msg.obj = infoo;
        }else{
        msg.what = 0;//失敗
        msg.obj = "2";
        }
       } catch (Exception e) {
       e.printStackTrace();
     msg.what = -1;
     msg.obj = e;
       }
    handler.sendMessage(msg);
    }
    }.start();
    }
 }



package com.itcast.test03;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
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.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Post {
 
 public String logina(String name, String pwd)throws ParseException, IOException, JSONException{
   //獲取到HttpClient對(duì)象
   HttpClient httpClient = new DefaultHttpClient();
   String strurl = "http://10.6.78.213:2016/xampp/sse/index.php/home/Index/server_info";
   HttpPost request = new HttpPost(strurl);
   request.addHeader("Accept","application/json");
   request.addHeader("Content-Type","application/json");//還可以自定義增加header
   JSONObject param = new JSONObject();//定義json對(duì)象
   param.put("sequenceId", "87620056570355357690");
   param.put("accType", "0");
   param.put("loginId", name);
   param.put("password", pwd);
   //param.put("thirdpartyAppId", "");
   //param.put("thirdpartyAccessToken", "");
   param.put("loginType", "1");
   Log.i("===========", param.toString());
   System.out.println("1+===========");
   StringEntity se = new StringEntity(param.toString());
   request.setEntity(se);//發(fā)送數(shù)據(jù)
   HttpResponse httpResponse = httpClient.execute(request);//獲得相應(yīng)
   System.out.println("2+===========");//進(jìn)行一行一行的調(diào)試時(shí)無(wú)法打印此語(yǔ)句。原因就是無(wú)法成功連接到                        網(wǎng)絡(luò)
   int code = httpResponse.getStatusLine().getStatusCode();
   System.out.print(code);
   String result = EntityUtils.toString(httpResponse.getEntity());
   JSONObject result1 = new JSONObject(result);
   String info = (String) result1.get("retInfo");
   Log.i("=============", info);
   return info;
   }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論