Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ie.example.artur.adminapp;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewConfiguration;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.google.gson.JsonObject;
- import java.lang.reflect.Field;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- import retrofit2.Retrofit;
- import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
- public class MainActivity extends AppCompatActivity {
- EditText editTextName,editTextEmail,editTextPassword;
- TextView textView;
- private static final String DB_URL = "jdbc:mysql://127.0.0.1/arturo";
- private static final String USER = "root";
- private static final String PASS = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- textView = (TextView) findViewById(R.id.textView);
- editTextName = (EditText) findViewById(R.id.editTextName);
- editTextEmail = (EditText) findViewById(R.id.editTextEmail);
- editTextPassword = (EditText) findViewById(R.id.editTextPassword);
- //// try {
- //// ViewConfiguration config = ViewConfiguration.get(this);
- //// Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
- //// if (menuKeyField != null) {
- //// menuKeyField.setAccessible(true);
- //// menuKeyField.setBoolean(config, false);
- //// }
- // } catch (Exception ignored) {
- // }
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- //Register user on button click
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- registerUser();
- }
- });
- }
- //
- // public void btnConn(View view) {
- // Send objSend = new Send();
- // objSend.execute("");
- //
- //
- // }
- private class Send extends AsyncTask<String, String, String>
- {
- String msg = "";
- String name = editTextName.getText().toString();
- String email = editTextEmail.getText().toString();
- String password = editTextPassword.getText().toString();
- @Override
- protected void onPreExecute() {
- textView.setText("Please Wait Inserting Data");
- }
- @Override
- protected String doInBackground(String... strings) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
- if (conn == null) {
- msg = "Connection goes wrong";
- } else {
- String query = "Insert INTO users (name,email,password) VALUES('" + name+"','"+email+"','"+password+"')";
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(query);
- msg = "Inserting Successful!!";
- }
- conn.close();
- }
- catch(
- Exception e
- )
- {
- msg = "Connection goes Wrong";
- e.printStackTrace();
- }
- return msg;
- }
- @Override
- protected void onPostExecute(String msg) {textView.setText(msg);}
- }
- //Register user by using the API
- private void registerUser(){
- //Get input data
- String email = editTextEmail.getText().toString();
- String password = editTextPassword.getText().toString();
- String name = editTextName.getText().toString();
- ApiClient apiClient = new ApiClient();
- //Register user to the API and pass input data as parameters using retrofit
- Call<JsonObject> call = apiClient.apiService.register(email, name, password);
- call.enqueue(new Callback<JsonObject>() {
- @Override
- public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
- //Handle success
- if(response.code() == 200){
- textView.setText("Inserting Successful!!");
- }
- //Any other code than 200 means that the insertion was not successful.
- else{
- textView.setText("Inserting failed!!");
- }
- }
- @Override
- public void onFailure(Call<JsonObject> call, Throwable t) {
- //Handle failure
- textView.setText("Inserting failed!!");
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- {
- switch (item.getItemId())
- {
- case R.id.action_settings : startActivity (new Intent(this, ShowUsers.class));
- break;
- }
- return super.onOptionsItemSelected(item);
- }}
- }
- public void addUsernamesToListView(){
- ApiClient apiClient = new ApiClient();
- Call<JsonObject> call = apiClient.apiService.getUsers();
- call.enqueue(new Callback<JsonObject>() {
- @Override
- public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
- JsonArray jsonArray = response.body().get("data").getAsJsonArray();
- //Arraylist, which will hold the usernames
- ArrayList<String> usernames = new ArrayList<String>();
- //Adds usernames by iterating the JSON-data
- for(int i=0; i<jsonArray.size(); i++){
- User user = new Gson().fromJson(jsonArray.get(i).toString(), User.class);
- usernames.add(user.getName());
- }
- //Add usernames to listview
- lv.setAdapter(new ArrayAdapter<String>(ShowUsers.this,android.R.layout.simple_list_item_1,usernames));
- }
- @Override
- public void onFailure(Call<JsonObject> call, Throwable t) {
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement