Android : 简单login

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStream;
  4 import java.io.InputStreamReader;
  5 import java.io.UnsupportedEncodingException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import org.apache.http.HttpEntity;
  9 import org.apache.http.HttpResponse;
 10 import org.apache.http.HttpStatus;
 11 import org.apache.http.client.ClientProtocolException;
 12 import org.apache.http.client.HttpClient;
 13 import org.apache.http.client.entity.UrlEncodedFormEntity;
 14 import org.apache.http.client.methods.HttpGet;
 15 import org.apache.http.client.methods.HttpPost;
 16 import org.apache.http.impl.client.DefaultHttpClient;
 17 import org.apache.http.message.BasicNameValuePair;
 18 import org.omg.CORBA.NameValuePair;
 19 import android.app.Activity;
 20 import android.content.Context;
 21 import android.content.Intent;
 22 import android.content.SharedPreferences;
 23 import android.content.res.Resources;
 24 import android.os.Bundle;
 25 import android.util.Log;
 26 import android.view.View;
 27 import android.widget.Button;
 28 import android.widget.EditText;
 29 import android.widget.Toast;
 30 
 31 public class LoginActivity extends Activity {
 32     private EditText edittext_UserName;
 33     private EditText edittext_UserPassword;
 34     private Button btn_login;
 35     private String username, password;
 36 
 37     @Override
 38     protected void onCreate(Bundle savedInstanceState) {
 39         super.onCreate(savedInstanceState);
 40         setContentView(R.layout.login);
 41         edittext_UserName = (EditText) findViewById(R.id.edittext_UserName);
 42         edittext_UserPassword = (EditText) findViewById(R.id.edittext_UserPassword);
 43         btn_login = (Button) findViewById(R.id.btn_login);
 44         btn_login.setOnClickListener(new View.OnClickListener() {
 45 
 46             @Override
 47             public void onClick(View v) {
 48                 // TODO Auto-generated method stub
 49                 username = edittext_UserName.getText().toString();
 50                 password = edittext_UserPassword.getText().toString();
 51                 doLogin();
 52             }
 53         });
 54     }
 55 
 56     void doLogin() {
 57         Resources res = getResources();
 58         if (username != null && password != null
 59                 && !res.getString(R.string.hint_name).equals(username)
 60                 && !res.getString(R.string.hint_password).equals(password)) {
 61 
 62             String url = "http://www.zbwen.com/testPrj/login.action";
 63             List<NameValuePair> params = new ArrayList<NameValuePair>();
 64             params.add(new BasicNameValuePair("user.name", username));
 65             params.add(new BasicNameValuePair("user.password", password));
 66 
 67             String result = HttpUtil.sendPostHttpRequest(url, params);
 68             if (result != null && result.equals("success")) {
 69                 Storage.saveString(this"username", username);
 70                 Storage.saveString(this"password", password);
 71                 Intent intent = new Intent();
 72                 intent.setClass(LoginActivity.this, WelcomeActivity.class);
 73                 LoginActivity.this.startActivity(intent);
 74                 Toast.makeText(LoginActivity.this"Login success",
 75                         Toast.LENGTH_LONG).show();
 76             } else {
 77                 System.out.println("fail the request");
 78             }
 79         }
 80         else{
 81             System.out.println("fail the request");            
 82         }
 83         return;
 84     }
 85 
 86 }
 87 
 88 class HttpUtil {
 89 
 90     private static HttpClient httpClient = null;
 91 
 92     public static HttpClient getHttpClient() {
 93         if (httpClient == null) {
 94             httpClient = new DefaultHttpClient();
 95         }
 96         return httpClient;
 97     }
 98 
 99     public static String getJSONData(String url)
100             throws ClientProtocolException, IOException {
101         String result = "";
102         HttpGet httpGet = new HttpGet(url);
103         HttpClient httpClient = getHttpClient();
104         HttpResponse httpResponse = null;
105 
106         try {
107             httpResponse = httpClient.execute(httpGet);
108             HttpEntity httpEntity = httpResponse.getEntity();
109             if (httpEntity != null) {
110                 InputStream inputStream = httpEntity.getContent();
111                 result = convertStreamToString(inputStream);
112             }
113         } catch (ClientProtocolException e) {
114             // TODO Auto-generated catch block
115             e.printStackTrace();
116         } catch (IOException e) {
117             throw e;
118         } finally {
119             httpClient.getConnectionManager().shutdown();
120             httpResponse = null;
121         }
122         return result;
123 
124     }
125 
126     public static String sendPostHttpRequest(String url,
127             List<NameValuePair> params) {
128         HttpPost httpPost = new HttpPost(url);
129         try {
130             // HttpEntity httpEntity = new UrlEncodedFormEntity(params,"utf-8");
131             // httpPost.setEntity(httpEntity);
132             HttpClient httpClient = getHttpClient();
133             HttpResponse httpResponse = httpClient.execute(httpPost);
134             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
135                 String result = "";
136                 if (httpResponse.getEntity() != null) {
137                     InputStream is = httpResponse.getEntity().getContent();
138                     result = convertStreamToString(is);
139                     return result.trim();
140                 }
141             } else {
142                 System.out.println("fail the request");
143             }
144 
145         } catch (Exception e) {
146             e.printStackTrace();
147         }
148         return null;
149     }
150 
151     public static String convertStreamToString(InputStream is) {
152         BufferedReader reader = null;
153         try {
154             reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),
155                     512 * 1024);
156         } catch (UnsupportedEncodingException e1) {
157 
158             e1.printStackTrace();
159         }
160         StringBuilder sb = new StringBuilder();
161 
162         String line = null;
163         try {
164             while ((line = reader.readLine()) != null) {
165                 sb.append(line + "\n");
166             }
167         } catch (IOException e) {
168             Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),
169                     e);
170         } finally {
171             try {
172                 is.close();
173             } catch (IOException e) {
174                 e.printStackTrace();
175             }
176         }
177         return sb.toString();
178     }
179 }
180 
181 class Storage {
182     private static SharedPreferences getSharedPreferences(Context context) {
183         SharedPreferences sharedPreferences = context.getSharedPreferences(
184                 "userInfo", Context.MODE_WORLD_READABLE);
185         return sharedPreferences;
186     }
187 
188     public static void saveString(Context context, String key, String value) {
189         SharedPreferences sharedPreferences = getSharedPreferences(context);
190         sharedPreferences.edit().putString(key, value).commit();
191 
192     }
193 
194     public static String getString(Context context, String key) {
195         return getSharedPreferences(context).getString(key, "");
196     }
197 }

posted on 2011-07-18 15:45  周碧文  阅读(488)  评论(0编辑  收藏  举报

导航

www.zbwen.com