Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.utils.URLEncodedUtils;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- public class login_act extends Activity {
- private ProgressDialog pDialog;
- List<NameValuePair> params=null;
- static String response = null;
- private String url = "http://hostname_ip/rest-api/xxxxx/?format=json";
- static String u="";
- static String p="";
- String temp= "";
- // User name
- private EditText et_Username;
- // Password
- private EditText et_Password;
- // Sign In
- private Button bt_SignIn;
- // Message
- private TextView tv_Message;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login_layout);
- // Initialization
- et_Username = (EditText) findViewById(R.id.u_name);
- et_Password = (EditText) findViewById(R.id.password);
- bt_SignIn = (Button) findViewById(R.id.sign_in);
- tv_Message = (TextView) findViewById(R.id.statusop);
- bt_SignIn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- // Stores User name
- String username = String.valueOf(et_Username.getText());
- u=username;
- // Stores Password
- String password = String.valueOf(et_Password.getText());
- p=password;
- new Getlogin().execute();
- }
- });
- }
- private class Getlogin extends AsyncTask<Void, Void, Void> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- // Showing progress dialog
- pDialog = new ProgressDialog(login_act.this);
- pDialog.setMessage("Signing In...");
- pDialog.setCancelable(false);
- pDialog.show();
- }
- @Override
- protected Void doInBackground(Void... arg0) {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpEntity httpEntity = null;
- HttpResponse httpResponse = null;
- String credentials = u + ":" + p;
- try {
- if (params != null) {
- String paramString = URLEncodedUtils
- .format(params, "utf-8");
- url += "?" + paramString;
- }
- HttpGet httpGet = new HttpGet(url);
- String base64EncodedCredentials = Base64.encodeBytes(credentials.getBytes());
- httpGet.addHeader("Authorization", "Basic " + base64EncodedCredentials);
- httpResponse = httpClient.execute(httpGet);
- httpEntity = httpResponse.getEntity();
- temp = response = EntityUtils.toString(httpEntity);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if (pDialog.isShowing())
- try {
- pDialog.dismiss();
- if(temp.contains("count")){
- tv_Message.setText("Logged In");
- Intent go = new Intent(login_act.this,Scnd.class);
- Bundle extras = new Bundle();
- extras.putString("status", response);
- extras.putString("user", u);
- extras.putString("pass", p);
- // 4. add bundle to intent
- go.putExtras(extras);
- startActivity(go);
- finish();
- }else
- tv_Message.setText("Invalid username or password");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement