亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用http實現(xiàn)注冊登錄功能

 更新時間:2022年04月24日 12:38:41   作者:Banboofly  
這篇文章主要為大家詳細介紹了Android使用http實現(xiàn)注冊登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在項目中實現(xiàn)注冊登錄有很多種方式,一般對于初學者來說,不使用框架,采用http的post和get請求后臺服務器,是一種更好理解底層源碼的方式。使用框架實現(xiàn)注冊登錄雖然比自己封裝post和get請求后臺方便,但是不利于我們更好地理解其中的原理和機制。

實現(xiàn)的步驟大致分為以下幾點:

1. 創(chuàng)建HttpPost對象,并將服務器接口地址url設(shè)置好。
2. 利用NameValuePair類設(shè)置相關(guān)參數(shù),并將NameValuePair放入到list集合中。
3. 發(fā)起post請求獲取返回實例HttpResponse。
4. 使用EntityUtils對返回值的實體進行處理(可以取得返回的字符串,也可以取得返回的byte數(shù)組),一般在服務器返回的都是json字符串。

注意事項:

1.在主線程中不能直接訪問網(wǎng)絡,要開辟子線程。
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";//服務器接口地址
? ? private EditText name,pwd;//用戶名和密碼
? ? private Button submit;//提交按鈕
? ? private TextView result;//服務器返回結(jié)果
? ? //Handler用于接收服務端返回的數(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("服務器返回: " + 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) {
? /**
? ? * 開辟一個子線程訪問網(wǎng)絡,否則會拋出異常
? ? */
? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 將請求體內(nèi)容加入請求中
? ? ? ? ? ? ? ? ? ? ? ? ? ? httpPost.setEntity(requestHttpEntity);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 需要客戶端對象來發(fā)送請求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpClient httpClient = new DefaultHttpClient();
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發(fā)送請求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示響應
? ? ? ? ? ? ? ? ? ? ? ? ? ? showResponseResult(response);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }.start();

? ? ? ? ? ? }
? ? ? ? });

? ? }
? ? /**
? ? ?* 顯示響應結(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);
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 把服務器返回的結(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>

運行截圖:

網(wǎng)絡權(quán)限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android常用對話框使用大全

    Android常用對話框使用大全

    我們隨處可見對話框,該怎么制作?這篇文章主要為大家詳細介紹了Android常用對話框使用大全,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 輕松實現(xiàn)可擴展自定義的Android滾輪時間選擇控件

    輕松實現(xiàn)可擴展自定義的Android滾輪時間選擇控件

    這篇文章主要為大家詳細介紹了可擴展自定義的Android滾輪時間選擇控件,結(jié)合WheelView實現(xiàn)滾輪選擇日期操作,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android中EditText禁止輸入表情的實例代碼

    Android中EditText禁止輸入表情的實例代碼

    本篇文章主要介紹了Android中EditText禁止輸入表情的實例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • Flutter模仿實現(xiàn)微信底部導航欄流程詳解

    Flutter模仿實現(xiàn)微信底部導航欄流程詳解

    這篇文章主要介紹了Flutter模仿實現(xiàn)微信底部導航欄流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-05-05
  • Android小掛件(APP Widgets)設(shè)計指導

    Android小掛件(APP Widgets)設(shè)計指導

    這篇文章主要為大家詳細介紹了Android小掛件APP Widgets設(shè)計指導,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android內(nèi)置的OkHttp用法介紹

    Android內(nèi)置的OkHttp用法介紹

    okhttp是一個第三方類庫,用于android中請求網(wǎng)絡。這是一個開源項目,是安卓端最火熱的輕量級框架,由移動支付Square公司貢獻(該公司還貢獻了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient
    2022-08-08
  • Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解

    Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法詳解

    這篇文章主要介紹了Android開發(fā)中使用WebView控件瀏覽網(wǎng)頁的方法,結(jié)合實例形式較為詳細的總結(jié)分析了Android WebView控件的功能、布局、設(shè)置、常用方法及相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Android NavigationBar問題處理的方法

    Android NavigationBar問題處理的方法

    本篇文章主要介紹了Android NavigationBar問題處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • 解決Android Studio 出現(xiàn)“Cannot resolve symbol” 的問題

    解決Android Studio 出現(xiàn)“Cannot resolve symbo

    今天在調(diào)試的時候,Android Studio報了一個莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因為這東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下
    2023-03-03
  • Android實現(xiàn)二級列表購物車功能

    Android實現(xiàn)二級列表購物車功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)二級列表購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論