Android使用http實(shí)現(xiàn)注冊(cè)登錄功能
在項(xiàng)目中實(shí)現(xiàn)注冊(cè)登錄有很多種方式,一般對(duì)于初學(xué)者來(lái)說(shuō),不使用框架,采用http的post和get請(qǐng)求后臺(tái)服務(wù)器,是一種更好理解底層源碼的方式。使用框架實(shí)現(xiàn)注冊(cè)登錄雖然比自己封裝post和get請(qǐng)求后臺(tái)方便,但是不利于我們更好地理解其中的原理和機(jī)制。
實(shí)現(xiàn)的步驟大致分為以下幾點(diǎn):
1. 創(chuàng)建HttpPost對(duì)象,并將服務(wù)器接口地址url設(shè)置好。
2. 利用NameValuePair類(lèi)設(shè)置相關(guān)參數(shù),并將NameValuePair放入到list集合中。
3. 發(fā)起post請(qǐng)求獲取返回實(shí)例HttpResponse。
4. 使用EntityUtils對(duì)返回值的實(shí)體進(jìn)行處理(可以取得返回的字符串,也可以取得返回的byte數(shù)組),一般在服務(wù)器返回的都是json字符串。
注意事項(xiàng):
1.在主線程中不能直接訪問(wèn)網(wǎng)絡(luò),要開(kāi)辟子線程。
2.在子線程中不能直接更新ui。
MainActivity:
package wujie.com.myapplication11;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
? ? private String url="http://192.168.1.101:8080/SHproject/homepage/register";//服務(wù)器接口地址
? ? private EditText name,pwd;//用戶名和密碼
? ? private Button submit;//提交按鈕
? ? private TextView result;//服務(wù)器返回結(jié)果
? ? //Handler用于接收服務(wù)端返回的數(shù)據(jù)更新ui
? ? private Handler hanlder=new Handler(){
? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? switch (msg.what) {
? ? ? ? ? ? ? ? case 0:
? ? ? ? ? ? ? ? ? ? String qq= (String) msg.obj;
? ? ? ? ? ? ? ? ? ? result.setText("服務(wù)器返回: " + qq);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? }
? ? };
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //初始化數(shù)據(jù)
? ? ? ? name= (EditText) findViewById(R.id.name);
? ? ? ? pwd= (EditText) findViewById(R.id.pwd);
? ? ? ? submit= (Button) findViewById(R.id.submit);
? ? ? ? result= (TextView) findViewById(R.id.result);
? ? ? ? submit.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? /**
? ? * 開(kāi)辟一個(gè)子線程訪問(wèn)網(wǎng)絡(luò),否則會(huì)拋出異常
? ? */
? ? ? ? ? ? ? ? new Thread() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? ? ? String name1=name.getText().toString().trim();
? ? ? ? ? ? ? ? ? ? ? ? String pwd1=pwd.getText().toString().trim();
? ? ? ? ? ? ? ? ? ? ? ? NameValuePair pair1 = new BasicNameValuePair("name", name1);
? ? ? ? ? ? ? ? ? ? ? ? NameValuePair pair2 = new BasicNameValuePair("password", pwd1);
? ? ? ? ? ? ? ? ? ? ? ? List<NameValuePair> pairList = new ArrayList<NameValuePair>();
? ? ? ? ? ? ? ? ? ? ? ? pairList.add(pair1);
? ? ? ? ? ? ? ? ? ? ? ? pairList.add(pair2);
? ? ? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpEntity requestHttpEntity = new UrlEncodedFormEntity(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pairList);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // URl是接口地址
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpPost httpPost = new HttpPost(url);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 將請(qǐng)求體內(nèi)容加入請(qǐng)求中
? ? ? ? ? ? ? ? ? ? ? ? ? ? httpPost.setEntity(requestHttpEntity);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 需要客戶端對(duì)象來(lái)發(fā)送請(qǐng)求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpClient httpClient = new DefaultHttpClient();
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發(fā)送請(qǐng)求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示響應(yīng)
? ? ? ? ? ? ? ? ? ? ? ? ? ? showResponseResult(response);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }.start();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 顯示響應(yīng)結(jié)果到命令行和TextView
? ? ?* @param response
? ? ?*/
? ? private void showResponseResult(HttpResponse response)
? ? {
? ? ? ? if (null == response)
? ? ? ? {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? HttpEntity httpEntity = response.getEntity();
? ? ? ? try
? ? ? ? {
? ? ? ? ? ? InputStream inputStream = httpEntity.getContent();
? ? ? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(
? ? ? ? ? ? ? ? ? ? inputStream));
? ? ? ? ? ? String result1 = "";
? ? ? ? ? ? String line = "";
? ? ? ? ? ? while (null != (line = reader.readLine()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? result1 += line;
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(result1);
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 把服務(wù)器返回的結(jié)果 發(fā)送到hanlder中,在子線程中是不允許更新ui的
? ? ? ? ? ? ?*/
? ? ? ? ? ? hanlder.obtainMessage(0,result1).sendToTarget();
? ? ? ? }
? ? ? ? catch (Exception e)
? ? ? ? {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:paddingBottom="@dimen/activity_vertical_margin" ? ? android:paddingLeft="@dimen/activity_horizontal_margin" ? ? android:paddingRight="@dimen/activity_horizontal_margin" ? ? android:paddingTop="@dimen/activity_vertical_margin" ? ? tools:context="wujie.com.myapplication11.MainActivity" ? ? android:orientation="vertical"> ? ? <EditText ? ? ? ? android:id="@+id/name" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:hint="用戶名" ? ? ? ? /> ? ? <EditText ? ? ? ? android:id="@+id/pwd" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:hint="密碼" ? ? ? ? /> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/submit" ? ? ? ? android:text="提交"/> ? ? <TextView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/result" ? ? ? ? android:textColor="#ff0000" ? ? ? ? android:textSize="20sp" ? ? ? ? android:paddingTop="18dp"/> </LinearLayout>
運(yùn)行截圖:

網(wǎng)絡(luò)權(quán)限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷(xiāo)功能
- android實(shí)現(xiàn)注冊(cè)登錄程序
- Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面(下)
- Android實(shí)現(xiàn)登錄注冊(cè)功能
- Android基于Sqlite實(shí)現(xiàn)注冊(cè)和登錄功能
- Android實(shí)現(xiàn)注冊(cè)登錄界面的實(shí)例代碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
- Android登錄注冊(cè)功能 數(shù)據(jù)庫(kù)SQLite驗(yàn)證
- Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)
相關(guān)文章
Fragment里添加ListView不要用ListFragment
這篇文章主要介紹了Fragment里添加ListView不要用ListFragment,需要的朋友可以參考下2015-07-07
超過(guò)百萬(wàn)的StackOverflow Flutter 20大問(wèn)題(推薦)
這篇文章主要介紹了超過(guò)百萬(wàn)的StackOverflow Flutter 問(wèn)題,有的問(wèn)題在stackoverflow上有幾十萬(wàn)的閱讀量,說(shuō)明很多人都遇到了這些問(wèn)題,把這些問(wèn)題整理分享給大家需要的朋友可以參考下2020-04-04
Android開(kāi)發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢(shì)
這篇文章主要給大家介紹了關(guān)于Android開(kāi)發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android?APN數(shù)據(jù)庫(kù)查詢(xún)對(duì)比分析(APN案例)
文章詳細(xì)介紹了Android中APN數(shù)據(jù)查詢(xún)的實(shí)現(xiàn)方式,文章說(shuō)明了如何避免在主線程進(jìn)行IO操作,從而提高應(yīng)用的響應(yīng)性和用戶體驗(yàn),感興趣的朋友一起看看吧2025-03-03
android 控件同時(shí)監(jiān)聽(tīng)單擊和雙擊實(shí)例
這篇文章主要介紹了android 控件同時(shí)監(jiān)聽(tīng)單擊和雙擊實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例的相關(guān)資料,Android 圓角圖片的實(shí)現(xiàn)形式,包括用第三方、也有系統(tǒng),需要的朋友可以參考下2017-07-07
listview與SQLite結(jié)合實(shí)現(xiàn)記事本功能
這篇文章主要為大家詳細(xì)介紹了listview與SQLite結(jié)合實(shí)現(xiàn)記事本功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn)
本文主要介紹了手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03

