android - 使用 JSON 使用 Networking GET 方法登录应用程序
问题描述
我对 Android 及其一些核心概念相当陌生。我正在寻找一个基本的登录屏幕。为了让用户登录 http 函数 (GET) 方法,必须使用 JSON 对象向服务器验证凭据。用户有 2 个登录选项。
考官登录信息:
- 用户名:管理员
- 密码:admin
用户登录信息:
- 用户名:用户
- 密码:12345
服务器:http ://mohameom.dev.fast.sheridanc.on.ca/users/verifyUserData.php?name=user&password=12345
我在这里先向您的帮助表示感谢!
怎么做呢?
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtSignin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Login"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@color/colorAccent" />
<EditText
android:id="@+id/edtUser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="30dp"
android:text=""
android:hint="Username"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
<EditText
android:id="@+id/edtPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="30dp"
android:ems="10"
android:inputType="textPassword"
android:text=""
android:hint="Password"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="30dp"
android:text="Login"
android:onClick="loginUser"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
</LinearLayout>
解决方案
HTTP 连接默认禁止,你应该允许它android:usesCleartextTraffic="true"
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:usesCleartextTraffic="true" android:theme="@style/AppTheme" > <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
登录活动(Kotlin,从文档中复制过去 -> https://developer.android.com/training/volley/simple#kotlin)
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.login) btnLogin.setOnClickListener { val queue = Volley.newRequestQueue(this) val url = "http://mohameom.dev.fast.sheridanc.on.ca/users/verifyUserData.php?name=${edtUser.text}&password=${edtPass.text}" val stringRequest = StringRequest( Request.Method.GET, url, Response.Listener<String> { response -> txtSignin.text = response.toString() // Process response if needed }, Response.ErrorListener { txtSignin.text = "That didn't work!" }) queue.add(stringRequest) } }
推荐阅读
- amazon-web-services - Govcloud 私有托管区域中的 ACM 待验证
- php - 非法字符串偏移 'url' Wordpress
- sass - Visual Studio 2019 - 双击选择 scss 变量不再选择美元符号
- python - Python Socket编程服务器和多个客户端存储数据失败
- outlook - 尝试在 PC 上的浏览器中在我的 Google 日历上获取我的 Outlook 日历(可以通过其他方式进行)
- javascript - react-router-dom v6 在应该不渲染路由时渲染它
- c++ - C++ 中的原子和多线程
- linux - 在 Bash 中将一个变量分配给另一个变量
- machine-learning - 为什么CNN给出负维度ValueError?
- paypal - Sdk 中的 Paypal 自定义运费