首页 > 解决方案 > 使用 JSON 使用 Networking GET 方法登录应用程序

问题描述

我对 Android 及其一些核心概念相当陌生。我正在寻找一个基本的登录屏幕。为了让用户登录 http 函数 (GET) 方法,必须使用 JSON 对象向服务器验证凭据。用户有 2 个登录选项。

考官登录信息:

用户登录信息:

服务器: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>

标签: androidandroid-networkingandroid-json

解决方案


  1. 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>
    

  2. 登录活动(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)
      }
    }
    

推荐阅读