Params
,
Progress
, and
Result
,
and 4 steps: onPreExecute
, doInBackground
, onProgressUpdate
, and onPostExecute
.
The string ü@foo-barwould get converted to
The+string+%C3%BC%40foo-barbecause 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).
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 username = (String) arg0[0]; String password = (String) arg0[1]; String link = "http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/520/14/"; // Complete the URL. if ( byGetOrPost == 0 ) { // Get method link += "LoginGet.cgi"; link += "?username=" + URLEncoder.encode( username, "UTF-8" ); link += "&password=" + URLEncoder.encode( password, "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( "username", "UTF-8" ) + "="; data += URLEncoder.encode( username, "UTF-8" ) + "&"; data += URLEncoder.encode( "password", "UTF-8" ) + "="; data += URLEncoder.encode( password, "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 |