Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. package ie.example.artur.adminapp;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.os.Bundle;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.design.widget.Snackbar;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.Toolbar;
  10. import android.view.View;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.view.ViewConfiguration;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17.  
  18. import com.google.gson.JsonObject;
  19.  
  20. import java.lang.reflect.Field;
  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.sql.Statement;
  24.  
  25. import retrofit2.Call;
  26. import retrofit2.Callback;
  27. import retrofit2.Response;
  28. import retrofit2.Retrofit;
  29. import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
  30.  
  31. public class MainActivity extends AppCompatActivity {
  32.  
  33.  
  34. EditText editTextName,editTextEmail,editTextPassword;
  35. TextView textView;
  36. private static final String DB_URL = "jdbc:mysql://127.0.0.1/arturo";
  37. private static final String USER = "root";
  38. private static final String PASS = "";
  39.  
  40.  
  41.  
  42. @Override
  43. protected void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.activity_register);
  46. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  47. setSupportActionBar(toolbar);
  48.  
  49. textView = (TextView) findViewById(R.id.textView);
  50. editTextName = (EditText) findViewById(R.id.editTextName);
  51. editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  52. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  53. //// try {
  54. //// ViewConfiguration config = ViewConfiguration.get(this);
  55. //// Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
  56. //// if (menuKeyField != null) {
  57. //// menuKeyField.setAccessible(true);
  58. //// menuKeyField.setBoolean(config, false);
  59. //// }
  60. // } catch (Exception ignored) {
  61. // }
  62.  
  63.  
  64. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  65. fab.setOnClickListener(new View.OnClickListener() {
  66. @Override
  67. public void onClick(View view) {
  68. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  69. .setAction("Action", null).show();
  70. }
  71. });
  72.  
  73.  
  74. //Register user on button click
  75. Button button = (Button) findViewById(R.id.button);
  76. button.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. registerUser();
  80. }
  81. });
  82.  
  83. }
  84. //
  85. // public void btnConn(View view) {
  86. // Send objSend = new Send();
  87. // objSend.execute("");
  88. //
  89. //
  90. // }
  91.  
  92.  
  93.  
  94.  
  95.  
  96. private class Send extends AsyncTask<String, String, String>
  97.  
  98. {
  99. String msg = "";
  100. String name = editTextName.getText().toString();
  101. String email = editTextEmail.getText().toString();
  102. String password = editTextPassword.getText().toString();
  103.  
  104. @Override
  105. protected void onPreExecute() {
  106. textView.setText("Please Wait Inserting Data");
  107. }
  108.  
  109. @Override
  110. protected String doInBackground(String... strings) {
  111. try {
  112. Class.forName("com.mysql.jdbc.Driver");
  113. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  114. if (conn == null) {
  115. msg = "Connection goes wrong";
  116. } else {
  117. String query = "Insert INTO users (name,email,password) VALUES('" + name+"','"+email+"','"+password+"')";
  118. Statement stmt = conn.createStatement();
  119. stmt.executeUpdate(query);
  120. msg = "Inserting Successful!!";
  121.  
  122. }
  123.  
  124. conn.close();
  125.  
  126. }
  127.  
  128. catch(
  129. Exception e
  130. )
  131.  
  132. {
  133. msg = "Connection goes Wrong";
  134. e.printStackTrace();
  135.  
  136. }
  137.  
  138. return msg;
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145. @Override
  146. protected void onPostExecute(String msg) {textView.setText(msg);}
  147.  
  148.  
  149.  
  150. }
  151.  
  152. //Register user by using the API
  153. private void registerUser(){
  154.  
  155. //Get input data
  156. String email = editTextEmail.getText().toString();
  157. String password = editTextPassword.getText().toString();
  158. String name = editTextName.getText().toString();
  159.  
  160. ApiClient apiClient = new ApiClient();
  161.  
  162. //Register user to the API and pass input data as parameters using retrofit
  163. Call<JsonObject> call = apiClient.apiService.register(email, name, password);
  164.  
  165. call.enqueue(new Callback<JsonObject>() {
  166.  
  167.  
  168. @Override
  169. public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  170. //Handle success
  171. if(response.code() == 200){
  172. textView.setText("Inserting Successful!!");
  173. }
  174.  
  175. //Any other code than 200 means that the insertion was not successful.
  176. else{
  177. textView.setText("Inserting failed!!");
  178. }
  179.  
  180. }
  181.  
  182. @Override
  183. public void onFailure(Call<JsonObject> call, Throwable t) {
  184. //Handle failure
  185. textView.setText("Inserting failed!!");
  186. }
  187. });
  188. }
  189.  
  190. @Override
  191. public boolean onCreateOptionsMenu(Menu menu)
  192. {
  193. // Inflate the menu; this adds items to the action bar if it is present.
  194. getMenuInflater().inflate(R.menu.menu_main, menu);
  195. return true;
  196. }
  197.  
  198. @Override
  199. public boolean onOptionsItemSelected(MenuItem item) {
  200. // Handle action bar item clicks here. The action bar will
  201. // automatically handle clicks on the Home/Up button, so long
  202. // as you specify a parent activity in AndroidManifest.xml.
  203. {
  204. switch (item.getItemId())
  205. {
  206. case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
  207. break;
  208. }
  209. return super.onOptionsItemSelected(item);
  210. }}
  211.  
  212. }
  213.  
  214. public void addUsernamesToListView(){
  215.  
  216. ApiClient apiClient = new ApiClient();
  217.  
  218. Call<JsonObject> call = apiClient.apiService.getUsers();
  219.  
  220. call.enqueue(new Callback<JsonObject>() {
  221.  
  222.  
  223. @Override
  224. public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  225.  
  226. JsonArray jsonArray = response.body().get("data").getAsJsonArray();
  227.  
  228. //Arraylist, which will hold the usernames
  229. ArrayList<String> usernames = new ArrayList<String>();
  230.  
  231. //Adds usernames by iterating the JSON-data
  232. for(int i=0; i<jsonArray.size(); i++){
  233. User user = new Gson().fromJson(jsonArray.get(i).toString(), User.class);
  234. usernames.add(user.getName());
  235. }
  236.  
  237. //Add usernames to listview
  238. lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,usernames));
  239.  
  240. }
  241.  
  242. @Override
  243. public void onFailure(Call<JsonObject> call, Throwable t) {
  244.  
  245. }
  246. });
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement