androidandroid-networkingandroid-json

Logging into app with Networking GET method using JSON


I'm fairly new to Android and some of its core concepts. I'm looking for a basic login screen. In order for the user to login http functions (GET) methods must be used to validate the credentials with the server using JSON object. The user has 2 login options.

Examiner Login Info:

User Login Info:

Server: http://mohameom.dev.fast.sheridanc.on.ca/users/verifyUserData.php?name=user&password=12345

Thanks for the help in advance!

How would one go about doing this?

Xml File:

<?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>

Solution

    1. HTTP connection forbidden on default, you should allow it 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. Login Activity (Kotlin, copypast from the documentation -> 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)
        }
      }