Sei sulla pagina 1di 3

import android.content.

Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Tambahuser extends AppCompatActivity {


EditText reg_nama;
EditText reg_password;
EditText reg_password_confirm;
Spinner vpilih;
Button reg_register;
String grup, get_nama, get_pass, confirm_pass;
String URL_LOGIN = "http://10.10.9.65/smp/register.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tambahuser);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.icontop);
getSupportActionBar().setDisplayUseLogoEnabled(true);
reg_nama=findViewById(R.id.namareg);
reg_password=findViewById(R.id.passnyareg);
reg_password_confirm = findViewById(R.id.confirmpassnyareg);
reg_register=findViewById(R.id.registrasi);
vpilih = (Spinner) findViewById(R.id.pilihreg);

reg_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*//membuka aktivitas baru
Intent register=new Intent(Tambahuser.this, Tambahuser.class);
startActivity(register);*/
//Toast.makeText(Tambahuser.this, "register",
Toast.LENGTH_LONG).show();
get_nama = reg_nama.getText().toString();
get_pass = reg_password.getText().toString();
confirm_pass = reg_password_confirm.getText().toString();

grup = vpilih.getSelectedItem().toString();
if (get_nama.equals("")) {
Toast.makeText(Tambahuser.this, "Field username kosong",
Toast.LENGTH_LONG).show();
} else if (get_pass.equals("")) {
Toast.makeText(Tambahuser.this, "Field password kosong",
Toast.LENGTH_LONG).show();
} else if (confirm_pass.equals("")) {
Toast.makeText(Tambahuser.this, "Field password kosong",
Toast.LENGTH_LONG).show();
}else if (!get_pass.equals(confirm_pass)) {
Toast.makeText(Tambahuser.this, "Password dan konfirmasi tidak
sesuai", Toast.LENGTH_LONG).show();
}else {
register(get_nama, get_pass, grup);
}
}
});
}

private void register(final String Username, final String Password, final


String Group) {
Log.i("proses login", "jalan");
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest contoh = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("hasil request", response);
try {
JSONObject obj = new JSONObject(response);
boolean success = obj.getBoolean("success");
if (success) {
JSONObject data = obj.getJSONObject("data");

Intent Tambahuser = new Intent(Tambahuser.this,


Mainortu.class);
startActivity(Tambahuser);

}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Toast.makeText(Tambahuser.this,error.getMessage(),Toast.LENGTH_LONG).show();
Log.e("hasil request", error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
// parameter request
Map<String, String> params = new HashMap<>();
params.put("username", Username);
params.put("password", Password);
params.put("id_group", Group);
return params;
}
};

requestQueue.add(contoh);

}
}