Java Source Code: SigninActivity.java


This class is used to connect to the server and includes the following three methods:
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 username = (String) arg0[0];
32      String password = (String) arg0[1];
33      String link     = "http://undcemcs01.und.edu/~wen.chen.hu/course/457/11/Android/";
34 
35      // Complete the URL.
36      if ( byGetOrPost == 0 ) { // Get method
37        link += "LoginGet.cgi";
38        link += "?username=" + URLEncoder.encode( username, "UTF-8" );
39        link += "&password=" + URLEncoder.encode( password, "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( "username", "UTF-8" ) + "=";
53               data += URLEncoder.encode( username,   "UTF-8" ) + "&";
54               data += URLEncoder.encode( "password", "UTF-8" ) + "=";
55               data += URLEncoder.encode( password,   "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}