網站首頁 編程語言 正文
在項目中實現注冊登錄有很多種方式,一般對于初學者來說,不使用框架,采用http的post和get請求后臺服務器,是一種更好理解底層源碼的方式。使用框架實現注冊登錄雖然比自己封裝post和get請求后臺方便,但是不利于我們更好地理解其中的原理和機制。
實現的步驟大致分為以下幾點:
1. 創建HttpPost對象,并將服務器接口地址url設置好。
2. 利用NameValuePair類設置相關參數,并將NameValuePair放入到list集合中。
3. 發起post請求獲取返回實例HttpResponse。
4. 使用EntityUtils對返回值的實體進行處理(可以取得返回的字符串,也可以取得返回的byte數組),一般在服務器返回的都是json字符串。
注意事項:
1.在主線程中不能直接訪問網絡,要開辟子線程。
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;//服務器返回結果
? ? //Handler用于接收服務端返回的數據更新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);
? ? ? ? //初始化數據
? ? ? ? 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) {
? /**
? ? * 開辟一個子線程訪問網絡,否則會拋出異常
? ? */
? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 將請求體內容加入請求中
? ? ? ? ? ? ? ? ? ? ? ? ? ? httpPost.setEntity(requestHttpEntity);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 需要客戶端對象來發送請求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpClient httpClient = new DefaultHttpClient();
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 發送請求
? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpResponse response = httpClient.execute(httpPost);
? ? ? ? ? ? ? ? ? ? ? ? ? ? // 顯示響應
? ? ? ? ? ? ? ? ? ? ? ? ? ? showResponseResult(response);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }.start();
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 顯示響應結果到命令行和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);
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 把服務器返回的結果 發送到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>
運行截圖:
網絡權限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
原文鏈接:https://blog.csdn.net/Banboofly/article/details/52946033
相關推薦
- 2022-03-16 Docker安裝Nginx問題及錯誤分析_docker
- 2022-09-13 iOS自定義相機功能_IOS
- 2023-03-29 Python-apply(lambda?x:?)的使用及說明_python
- 2022-04-22 教你如何解決Nginx禁止ip加端口訪問的問題_nginx
- 2022-05-05 python?scipy.spatial.distance?距離計算函數??_python
- 2022-07-18 RabbitMQ隊列阻塞該如何處理
- 2022-12-14 C++?Boost?ScopeExit超詳細講解_C 語言
- 2023-12-12 Mybatis中一些優化
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支