Ini adalah lanjutan dari Tutorial sebelumnya yaitu tentang Cara koneksi mysql dan android studio , dimana contoh yang diberikan untuk tes koneksinya adalah pembuatan form register user. Nah, di tutorial kali ini akan dijelaskan bagaimana membuat validasi agar username dan email yang sudah terdaftar tidak bisa lagi di daftarkan.
Membuat Request handler
Klik kanan com.ilmankadori.androidphpmysql-->New-->Java Class
Setelah itu daouble klik File Java RequestHandler dan ketikkan script berikut:
package com.ilmankadori.androidphpmysql;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class RequestHandler {
private static RequestHandler instance;
private RequestQueue requestQueue;
private static Context ctx;
private RequestHandler(Context context) {
ctx = context;
requestQueue = getRequestQueue();
}
public static synchronized RequestHandler getInstance(Context context) {
if (instance == null) {
instance = new RequestHandler(context);
}
return instance;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
requestQueue = Volley.newRequestQueue(ctx.getApplicationContext());
}
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
}
Setelah itu, Buka script MainActivity.java :
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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 MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextUsername, editTextEmail, editTextPassword;
private Button buttonRegister;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextEmail=(EditText) findViewById(R.id.editTextEmail);
editTextUsername=(EditText) findViewById(R.id.editTextUsername);
editTextPassword=(EditText) findViewById(R.id.editTextPassword);
buttonRegister=(Button) findViewById(R.id.buttonRegister);
progressDialog=new ProgressDialog(this);
buttonRegister.setOnClickListener(this);
}
private void registerUser(){
final String email=editTextEmail.getText().toString().trim();
final String username=editTextUsername.getText().toString().trim();
final String password=editTextPassword.getText().toString().trim();
progressDialog.setMessage("Registering user...");
progressDialog.show();
StringRequest stringRequest=new StringRequest(Request.Method.POST,
Constants.URL_REGISTER,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try{
JSONObject jsonObject=new JSONObject(response);
Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
}catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> params=new HashMap<>();
params.put("username", username);
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onClick(View view){
if (view==buttonRegister)
registerUser();
}
}
script yang di tulis dengan warna merah gantilah dengan script berikut
RequestHandler.getInstance(this).addToRequestQueue(stringRequest);
setelah itu bukalah script file Dboperations.php
Kemudian sesuaikan scriptnya menjadi seperti berikut:
Gambar 4. File DbOperations.php |
Setelah itu sesuaikan juga script di file registerUser.php menjadi seperti berikut:
Gambar 5. File registerUser.php |
Selamat Mencoba
Silahkan berkomentar jika ada yang ditanyakan.
0 comments:
Post a Comment