SigninActivity.java (Cont.)

import android.os.AsyncTask;
It allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by 3 generic types: Params, Progress, and Result, and 4 steps: onPreExecute, doInBackground, onProgressUpdate, and onPostExecute.

import java.net.URL;
It represents a Uniform Resource Locator, a pointer to a “resource” on the World Wide Web.

import java.net.URLConnection;
It is the superclass of all classes that represent a communications link between the application and a URL.

import java.net.URLEncoder;
It is a utility class for HTML form encoding, which converts characters into a format that can be transmitted over the Internet. For example, using UTF-8 as the encoding scheme the string
   The string ü@foo-bar
would get converted to
   The+string+%C3%BC%40foo-bar
because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).

import BufferedReader;
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Server/app/src/main/java/com/wenchen/server/SigninActivity.java
package com.wenchen.server;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class SigninActivity extends AsyncTask<String, Void, String> {
  private TextView statusField, roleField;
  private Context context;
  private int byGetOrPost = 0;

  // Flag 0 means GET and 1 means POST. (By default it is GET.)
  public SigninActivity( Context context, TextView statusField, TextView roleField, int flag ) {
    this.context     = context;
    this.statusField = statusField;
    this.roleField   = roleField;
    byGetOrPost      = flag;
  }

  protected void onPreExecute( ) { }

  @Override
  protected String doInBackground( String... arg0 ) {
    try {
      String name  = (String) arg0[0];
      String pword = (String) arg0[1];
      String link  = "http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/520/11/";

      // Complete the URL.
      if ( byGetOrPost == 0 ) { // Get method
        link += "LoginGet.cgi";
        link += "?name="  + URLEncoder.encode( name,  "UTF-8" );
        link += "&pword=" + URLEncoder.encode( pword, "UTF-8" );
      }
      else { // Post method
        link += "LoginPost.cgi";
      }

      // Connect to the server.
      URL url = new URL( link );
      URLConnection conn = url.openConnection( );
      conn.setDoOutput( true );

      // Send the arguments via standard output for the POST method.
      if ( byGetOrPost == 1 ) { // Post method
        String data  = URLEncoder.encode( "name",  "UTF-8" ) + "=";
               data += URLEncoder.encode( name,    "UTF-8" ) + "&";
               data += URLEncoder.encode( "pword", "UTF-8" ) + "=";
               data += URLEncoder.encode( pword,   "UTF-8" );
        OutputStreamWriter wr = new OutputStreamWriter(
          conn.getOutputStream( ) );
        wr.write( data );
        wr.flush( );
      }

      // Read server response.
      BufferedReader reader = new BufferedReader(
        new InputStreamReader( conn.getInputStream( ) ));
      StringBuilder sb = new StringBuilder( );
      String      line = null;
      while (( line = reader.readLine( ) ) != null ) {
        sb.append( line );
        break;
      }
      return sb.toString( );
    }
    catch( Exception e ) {
      return new String( "Exception: " + e.getMessage( ) );
    }
  }

  @Override
  protected void onPostExecute( String result ) {
    this.statusField.setText( "Login Successful" );
    this.roleField.setText  ( result );
  }
}




      The fool doth think he is wise,    
      but the wise man knows himself to be a fool.    
      ― William Shakespeare, As You Like It