SigninActivity.java (Cont.)

protected String doInBackground( String... arg0 ) {
It invoked on the background thread immediately after onPreExecute finishes executing. This step is used to perform background computation that can take a long time. The specified parameters are the parameters passed to execute(Params...) by the caller of this task. The result of the computation must be returned by this step and will be passed back to the last step.

URLEncoder.encode( username, "UTF-8" );
Translates a string into application/x-www-form-urlencoded format using the specific encoding scheme UTF-8.

URLConnection conn = url.openConnection( );
Returns a URLConnection instance that represents a connection to the remote object referred to by the URL.

conn.setDoOutput( true);
Sets the value of the doOutput field for this URLConnection to the specified value. A URL connection can be used for input and/or output. Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.

conn.getOutputStream( ) );
It returns an output stream that writes to this connection.

wr.flush( );
Flushes the stream and forces any buffered output to be written out.

protected void onPostExecute( String result ) {
Runs on the UI thread after doInBackground(Params...). The specified result is the value returned by doInBackground(Params...).
Server/app/src/main/java/com/wenchen/server/SigninActivity.java
01package com.wenchen.server;
02 
03import android.content.Context;
04import android.os.AsyncTask;
05import android.widget.TextView;
06import java.net.URL;
07import java.net.URLConnection;
08import java.net.URLEncoder;
09import java.io.BufferedReader;
10import java.io.InputStreamReader;
11import java.io.OutputStreamWriter;
12 
13public class SigninActivity extends AsyncTask<String, Void, String> {
14  private TextView statusField, roleField;
15  private Context context;
16  private int byGetOrPost = 0;
17 
18  // Flag 0 means GET and 1 means POST. (By default it is GET.)
19  public SigninActivity( Context context, TextView statusField, TextView roleField, int flag ) {
20    this.context     = context;
21    this.statusField = statusField;
22    this.roleField   = roleField;
23    byGetOrPost      = flag;
24  }
25 
26  protected void onPreExecute( ) { }
27 
28  @Override
29  protected String doInBackground( String... arg0 ) {
30    try {
31      String name  = (String) arg0[0];
32      String pword = (String) arg0[1];
33      String link  = "http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/520/11/";
34 
35      // Complete the URL.
36      if ( byGetOrPost == 0 ) { // Get method
37        link += "LoginGet.cgi";
38        link += "?name="  + URLEncoder.encode( name,  "UTF-8" );
39        link += "&pword=" + URLEncoder.encode( pword, "UTF-8" );
40      }
41      else { // Post method
42        link += "LoginPost.cgi";
43      }
44 
45      // Connect to the server.
46      URL url = new URL( link );
47      URLConnection conn = url.openConnection( );
48      conn.setDoOutput( true );
49 
50      // Send the arguments via standard output for the POST method.
51      if ( byGetOrPost == 1 ) { // Post method
52        String data  = URLEncoder.encode( "name""UTF-8" ) + "=";
53               data += URLEncoder.encode( name,    "UTF-8" ) + "&";
54               data += URLEncoder.encode( "pword", "UTF-8" ) + "=";
55               data += URLEncoder.encode( pword,   "UTF-8" );
56        OutputStreamWriter wr = new OutputStreamWriter(
57          conn.getOutputStream( ) );
58        wr.write( data );
59        wr.flush( );
60      }
61 
62      // Read server response.
63      BufferedReader reader = new BufferedReader(
64        new InputStreamReader( conn.getInputStream( ) ));
65      StringBuilder sb = new StringBuilder( );
66      String      line = null;
67      while (( line = reader.readLine( ) ) != null ) {
68        sb.append( line );
69        break;
70      }
71      return sb.toString( );
72    }
73    catch( Exception e ) {
74      return new String( "Exception: " + e.getMessage( ) );
75    }
76  }
77 
78  @Override
79  protected void onPostExecute( String result ) {
80    this.statusField.setText( "Login Successful" );
81    this.roleField.setText  ( result );
82  }
83}