Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. EditText editTextPhone;
  2. Button buttonRegister;
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8.  
  9. editTextPhone = (EditText) findViewById(R.id.editTextPhone);
  10. this.editTextPhone.setText("996");
  11. editTextPhone.setSelection(3);
  12.  
  13. buttonRegister = (Button) findViewById(R.id.buttonRegister);
  14. buttonRegister.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17.  
  18. Log.v("EditText", editTextPhone.getText().toString());
  19. //Log.v("EditText value=", editTextPhone.getText().toString());
  20. }
  21. });
  22. }
  23. }
  24.  
  25. import android.content.Intent;
  26. import android.content.SharedPreferences;
  27. import android.content.SharedPreferences.Editor;
  28. import android.os.AsyncTask;
  29. import android.os.Bundle;
  30. import android.preference.PreferenceManager;
  31. import android.support.v7.app.AppCompatActivity;
  32. import android.util.Log;
  33. import android.view.View;
  34. import android.view.View.OnClickListener;
  35. import android.widget.Button;
  36. import android.widget.EditText;
  37. import android.widget.Toast;
  38.  
  39. import org.apache.http.NameValuePair;
  40. import org.apache.http.message.BasicNameValuePair;
  41. import org.json.JSONException;
  42. import org.json.JSONObject;
  43.  
  44. import java.util.ArrayList;
  45. import java.util.List;
  46.  
  47. public class MainActivity extends AppCompatActivity implements OnClickListener {
  48. private EditText login , pass;
  49. private Button mSubmit, mRegister;
  50.  
  51.  
  52. private static String Data;
  53.  
  54. public static void NmSN(String data){
  55. Data = data;
  56. }
  57. // Progress Dialog
  58.  
  59. // JSON parser class
  60. JSONParser jsonParser = new JSONParser();
  61.  
  62. // php login script location:
  63.  
  64. // localhost :
  65. // testing on your device
  66. // put your local ip instead, on windows, run CMD > ipconfig
  67. // or in mac's terminal type ifconfig and look for the ip under en0 or en1
  68. // private static final String LOGIN_URL =
  69. // "http://xxx.xxx.x.x:1234/webservice/login.php";
  70.  
  71. // testing on Emulator:
  72. private static final String LOGIN_URL = "http://nw.pe.hu/******.php";
  73.  
  74. // testing from a real server:
  75. // private static final String LOGIN_URL =
  76. // "http://www.mybringback.com/webservice/login.php";
  77.  
  78. // JSON element ids from repsonse of php script:
  79. private static final String TAG_SUCCESS = "ifAuth";
  80. private static final String TAG_MESSAGE = "message";
  81.  
  82. @Override
  83. protected void onCreate(Bundle savedInstanceState) {
  84. // TODO Auto-generated method stub
  85. super.onCreate(savedInstanceState);
  86. setContentView(R.layout.activity_main);
  87.  
  88. // setup input fields
  89. login = (EditText) findViewById(R.id.username);
  90. pass = (EditText) findViewById(R.id.password);
  91.  
  92. // setup buttons
  93. mSubmit = (Button) findViewById(R.id.login);
  94. mRegister = (Button) findViewById(R.id.register);
  95.  
  96. // register listeners
  97. mSubmit.setOnClickListener(this);
  98. mRegister.setOnClickListener(this);
  99.  
  100. }
  101.  
  102. @Override
  103. public void onClick(View v) {
  104. // TODO Auto-generated method stubs
  105. switch (v.getId()) {
  106. case R.id.login:
  107. new AttemptLogin().execute();
  108. break;
  109.  
  110. default:
  111. break;
  112. }
  113. }
  114.  
  115. class AttemptLogin extends AsyncTask<String, String, String> {
  116.  
  117. @Override
  118. protected void onPreExecute() {
  119. super.onPreExecute();
  120.  
  121. }
  122.  
  123. @Override
  124. protected String doInBackground(String... args) {
  125. // TODO Auto-generated method stub
  126. // Check for success tag
  127. int success;
  128. String username = login.getText().toString();
  129. String password = pass.getText().toString();
  130. try {
  131. // Building Parameters
  132. List<NameValuePair> params = new ArrayList<NameValuePair>();
  133. params.add(new BasicNameValuePair("login", username));
  134. params.add(new BasicNameValuePair("pass", password));
  135.  
  136. Log.d("request!", "starting");
  137. // getting product details by making HTTP request
  138. JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
  139. params);
  140.  
  141. // check your log for json response
  142. Log.d("Login attempt", json.toString());
  143.  
  144. // json success tag
  145. success = json.getInt(TAG_SUCCESS);
  146. if (success == 1) {
  147.  
  148. Log.d("Login Successful!", json.toString());
  149. // save login data
  150.  
  151. SharedPreferences sp = PreferenceManager
  152. .getDefaultSharedPreferences(MainActivity.this);
  153. Editor edit = sp.edit();
  154. edit.putString("username", username);
  155. edit.commit();
  156. new AttemptGetData().execute(json.getString("ID"));
  157. JSONObject jsone = new JSONObject(Data);
  158. Intent i = new Intent(MainActivity.this, ProfileAct.class);
  159. i.putExtra("ID", json.getString("ID"));
  160. i.putExtra("name", jsone.getString("name"));
  161. i.putExtra("surname", jsone.getString("surname"));
  162. i.putExtra("avatar", jsone.getString("avatar"));
  163. finish();
  164. startActivity(i);
  165.  
  166.  
  167.  
  168. return json.getString(TAG_MESSAGE);
  169. } else {
  170. Toast.makeText(MainActivity.this, "Неверный логин и"или пароль!", Toast.LENGTH_SHORT).show();
  171. return json.getString(TAG_MESSAGE);
  172. }
  173. } catch (JSONException e) {
  174. e.printStackTrace();
  175. }
  176.  
  177. return null;
  178.  
  179. }
  180.  
  181. protected void onPostExecute(String file_url) {
  182. // dismiss the dialog once product deleted
  183.  
  184. if (file_url != null) {
  185. Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
  186. }
  187.  
  188. }
  189.  
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement