Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void login(final String username, final String password, final String url) {
- Log.d("HBmobile", "Passed to Login");
- Log.d("HBmobile", "Passed URL - " + url);
- final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
- final Button button = (Button) findViewById(R.id.button1);
- final EditText username2 = (EditText) findViewById(R.id.editText1);
- final EditText password2 = (EditText) findViewById(R.id.editText2);
- if ( htuser == null ) {
- final boolean htaccess = preferences.getBoolean("htcheck", false);
- final String htuser = preferences.getString("htuser", "");
- final String htpass = preferences.getString("htpass", "");
- }
- Credentials creds = null;
- if ( htaccess == true ) {
- creds = new UsernamePasswordCredentials(htuser, htpass);
- Log.d("HBmobile", "htaccess set - " + creds.toString());
- }
- Log.d("HBmobile", htuser + " : " + htpass);
- username2.setEnabled(false);
- password2.setEnabled(false);
- button.setEnabled(false);
- int response2 = 0;
- String reply = null;
- URL url2 = null;
- try {
- if (username == "TOKEN") {
- url2 = new URL(url + "/admin/api.php?&token=" + password + "&call=getHostBillversion" );
- } else {
- url2 = new URL(url + "/admin/api.php?&action=login&username=" + username + "&password=" + password);
- }
- Log.d("HBmobile", "New URL" + url2.toString());
- } catch (MalformedURLException e1) {
- e1.printStackTrace();
- }
- InputStream httpcontent=null;
- try{
- HttpGet httpGet = new HttpGet(url2.toURI());
- DefaultHttpClient httpclient = new DefaultHttpClient();
- if ( htaccess == true ) {
- httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
- }
- HttpResponse response = httpclient.execute(httpGet);
- response2 = response.getStatusLine().getStatusCode();
- if( response2 == 404) {
- Toast.makeText(getBaseContext(), "Not a valid HostBill Address.", Toast.LENGTH_SHORT).show();
- //loggingin.dismiss();
- AlertDialog alertDialog = new AlertDialog.Builder(main.this).create();
- alertDialog.setTitle("Error...");
- alertDialog.setMessage("An invalid URL has been set.\nPlease check the URL again.\nError: Status Code 404");
- alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- alertDialog.show();
- username2.setEnabled(true);
- password2.setEnabled(true);
- button.setEnabled(true);
- reply = "blanks";
- } else if (response2 == 200) {
- httpcontent = response.getEntity().getContent();
- BufferedReader rd = new BufferedReader(new InputStreamReader(httpcontent), 4096);
- String line;
- StringBuilder sb = new StringBuilder();
- while ((line = rd.readLine()) != null) {
- sb.append(line);
- }
- rd.close();
- reply = new String(sb.toString());
- response2 = response.getStatusLine().getStatusCode();
- } else if (response2 == 401) {
- Log.d("HBmobile", "401");
- final Dialog dialog = new Dialog(main.this);
- dialog.setContentView(R.layout.htaccessdialog);
- dialog.setTitle("401 - Authorization Required.");
- dialog.setCancelable(true);
- TextView message = (TextView) dialog.findViewById(R.id.textView3);
- final EditText htusername = (EditText) dialog.findViewById(R.id.editText1);
- final EditText htpassword = (EditText) dialog.findViewById(R.id.editText2);
- Button login = (Button) dialog.findViewById(R.id.button1);
- login.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- htaccess = true;
- htuser = htusername.getText().toString();
- htpass = htpassword.getText().toString();
- Log.d("HBmobile", htuser + " : " + htpass);
- login(username, password, url);
- dialog.dismiss();
- }
- });
- Button cancel = (Button) dialog.findViewById(R.id.button2);
- cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- dialog.dismiss();
- }
- });
- dialog.show();
- reply = "blanks";
- }
- Log.e("reply", reply);
- } catch (Exception e) {
- //loggingin.dismiss();
- Toast.makeText(getBaseContext(), "Invalid URL", Toast.LENGTH_SHORT);
- reply = "blanks";
- }
- if( reply.contains("success")){
- //loggingin.dismiss();
- int requestCode = 0;
- Intent i= new Intent(main.this, Home.class);
- Bundle b = new Bundle();
- if ( username == "TOKEN") {
- b.putString("token", password);
- } else {
- String token = reply.substring(reply.indexOf("token"),reply.indexOf(",\"call\""));
- token = token.substring(8);
- token = token.replace("\"", "");
- b.putString("token", token);
- }
- if ( htaccess = true ) {
- b.putBoolean("htaccess", true);
- b.putString("htuser", htuser);
- b.putString("htpass", htpass);
- } else {
- b.putBoolean("htaccess", false);
- }
- b.putString("url", url);
- i.putExtras(b);
- startActivityForResult(i, requestCode);
- } else if( reply.contains("error")){
- //loggingin.dismiss();
- if( reply.contains("ip_banned_login")){
- Toast.makeText(getBaseContext(), "Your IP has been banned.", Toast.LENGTH_LONG).show();
- } else if( reply.contains("invalid_token_foroperation")) {
- Toast.makeText(getBaseContext(), "Previous Session has expired.", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(getBaseContext(), "Username/Password incorrect", Toast.LENGTH_SHORT).show();
- }
- preferences.edit().remove("HB_TOKEN");
- username2.setEnabled(true);
- password2.setEnabled(true);
- button.setEnabled(true);
- } else {
- //loggingin.dismiss();
- Toast.makeText(getBaseContext(), "An unknown error occurred.", Toast.LENGTH_LONG).show();
- username2.setEnabled(true);
- password2.setEnabled(true);
- button.setEnabled(true);
- }
- }
Add Comment
Please, Sign In to add comment