Java Source Code: SigninActivity.java


This class is used to connect to the server by using AsyncTask. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. It includes the following three methods:
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 );
  }
}




      Teacher: Well, at least there’s one thing I can say about your son.    
      Father: What’s that?    
      Teacher: With grades like these, he couldn’t be cheating.