01 | package com.wenchen.server; |
03 | import android.content.Context; |
04 | import android.os.AsyncTask; |
05 | import android.widget.TextView; |
07 | import java.net.URLConnection; |
08 | import java.net.URLEncoder; |
09 | import java.io.BufferedReader; |
10 | import java.io.InputStreamReader; |
11 | import java.io.OutputStreamWriter; |
13 | public class SigninActivity extends AsyncTask<String, Void, String> { |
14 | private TextView statusField, roleField; |
15 | private Context context; |
16 | private int byGetOrPost = 0 ; |
19 | public SigninActivity( Context context, TextView statusField, TextView roleField, int flag ) { |
20 | this .context = context; |
21 | this .statusField = statusField; |
22 | this .roleField = roleField; |
26 | protected void onPreExecute( ) { } |
29 | protected String doInBackground( String... arg0 ) { |
31 | String name = (String) arg0[ 0 ]; |
32 | String pword = (String) arg0[ 1 ]; |
36 | if ( byGetOrPost == 0 ) { |
37 | link += "LoginGet.cgi" ; |
38 | link += "?name=" + URLEncoder.encode( name, "UTF-8" ); |
39 | link += "&pword=" + URLEncoder.encode( pword, "UTF-8" ); |
42 | link += "LoginPost.cgi" ; |
46 | URL url = new URL( link ); |
47 | URLConnection conn = url.openConnection( ); |
48 | conn.setDoOutput( true ); |
51 | if ( byGetOrPost == 1 ) { |
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( ) ); |
63 | BufferedReader reader = new BufferedReader( |
64 | new InputStreamReader( conn.getInputStream( ) )); |
65 | StringBuilder sb = new StringBuilder( ); |
67 | while (( line = reader.readLine( ) ) != null ) { |
71 | return sb.toString( ); |
73 | catch ( Exception e ) { |
74 | return new String( "Exception: " + e.getMessage( ) ); |
79 | protected void onPostExecute( String result ) { |
80 | this .statusField.setText( "Login Successful" ); |
81 | this .roleField.setText ( result ); |
|