Advertisement
Guest User

Untitled

a guest
Mar 5th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. import android.app.Activity;
  2.  
  3. import android.app.ProgressDialog;
  4.  
  5. import android.content.Intent;
  6.  
  7. import android.os.AsyncTask;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.view.View;
  12.  
  13. import android.view.View.OnClickListener;
  14.  
  15. import android.widget.Button;
  16.  
  17. import android.widget.EditText;
  18.  
  19. import android.widget.TextView;
  20.  
  21.  
  22. import org.apache.http.HttpEntity;
  23.  
  24. import org.apache.http.NameValuePair;
  25.  
  26. import org.apache.http.client.ClientProtocolException;
  27.  
  28. import org.apache.http.client.methods.HttpGet;
  29.  
  30. import org.apache.http.HttpResponse;
  31.  
  32. import org.apache.http.client.utils.URLEncodedUtils;
  33.  
  34. import org.apache.http.impl.client.DefaultHttpClient;
  35.  
  36. import org.apache.http.util.EntityUtils;
  37.  
  38. import java.io.IOException;
  39.  
  40. import java.io.UnsupportedEncodingException;
  41.  
  42. import java.util.List;
  43.  
  44.  
  45.  
  46. public class login_act extends Activity {
  47. private ProgressDialog pDialog;
  48.  
  49. List<NameValuePair> params=null;
  50. static String response = null;
  51.  
  52. private String url = "http://hostname_ip/rest-api/xxxxx/?format=json";
  53. static String u="";
  54. static String p="";
  55. String temp= "";
  56. // User name
  57. private EditText et_Username;
  58. // Password
  59. private EditText et_Password;
  60. // Sign In
  61. private Button bt_SignIn;
  62. // Message
  63. private TextView tv_Message;
  64.  
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.login_layout);
  69.  
  70.  
  71. // Initialization
  72. et_Username = (EditText) findViewById(R.id.u_name);
  73. et_Password = (EditText) findViewById(R.id.password);
  74. bt_SignIn = (Button) findViewById(R.id.sign_in);
  75. tv_Message = (TextView) findViewById(R.id.statusop);
  76.  
  77. bt_SignIn.setOnClickListener(new OnClickListener() {
  78.  
  79. @Override
  80. public void onClick(View view) {
  81. // Stores User name
  82. String username = String.valueOf(et_Username.getText());
  83. u=username;
  84.  
  85. // Stores Password
  86. String password = String.valueOf(et_Password.getText());
  87. p=password;
  88.  
  89. new Getlogin().execute();
  90.  
  91. }
  92.  
  93.  
  94. });
  95.  
  96.  
  97. }
  98.  
  99.  
  100. private class Getlogin extends AsyncTask<Void, Void, Void> {
  101.  
  102.  
  103. @Override
  104. protected void onPreExecute() {
  105. super.onPreExecute();
  106. // Showing progress dialog
  107. pDialog = new ProgressDialog(login_act.this);
  108. pDialog.setMessage("Signing In...");
  109. pDialog.setCancelable(false);
  110. pDialog.show();
  111. }
  112.  
  113. @Override
  114. protected Void doInBackground(Void... arg0) {
  115.  
  116.  
  117. DefaultHttpClient httpClient = new DefaultHttpClient();
  118. HttpEntity httpEntity = null;
  119. HttpResponse httpResponse = null;
  120.  
  121. String credentials = u + ":" + p;
  122.  
  123. try {
  124.  
  125. if (params != null) {
  126. String paramString = URLEncodedUtils
  127. .format(params, "utf-8");
  128. url += "?" + paramString;
  129. }
  130. HttpGet httpGet = new HttpGet(url);
  131.  
  132. String base64EncodedCredentials = Base64.encodeBytes(credentials.getBytes());
  133. httpGet.addHeader("Authorization", "Basic " + base64EncodedCredentials);
  134.  
  135. httpResponse = httpClient.execute(httpGet);
  136.  
  137. httpEntity = httpResponse.getEntity();
  138. temp = response = EntityUtils.toString(httpEntity);
  139.  
  140.  
  141.  
  142. } catch (UnsupportedEncodingException e) {
  143. e.printStackTrace();
  144. } catch (ClientProtocolException e) {
  145. e.printStackTrace();
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. }
  149.  
  150. return null;
  151.  
  152. }
  153.  
  154.  
  155. @Override
  156. protected void onPostExecute(Void result) {
  157. super.onPostExecute(result);
  158. // Dismiss the progress dialog
  159. if (pDialog.isShowing())
  160.  
  161. try {
  162. pDialog.dismiss();
  163. if(temp.contains("count")){
  164. tv_Message.setText("Logged In");
  165.  
  166. Intent go = new Intent(login_act.this,Scnd.class);
  167.  
  168.  
  169. Bundle extras = new Bundle();
  170. extras.putString("status", response);
  171. extras.putString("user", u);
  172. extras.putString("pass", p);
  173.  
  174. // 4. add bundle to intent
  175. go.putExtras(extras);
  176. startActivity(go);
  177.  
  178. finish();
  179.  
  180. }else
  181. tv_Message.setText("Invalid username or password");
  182.  
  183.  
  184. }
  185. catch (Exception e)
  186. {
  187. e.printStackTrace();
  188. }
  189.  
  190.  
  191.  
  192. }
  193.  
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement