Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wina.mongodbnodejs;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- 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.VolleyLog;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import server.ConfigURL;
- public class MainActivity extends AppCompatActivity {
- private RequestQueue mRequestQueue;
- private TextView txtdata;
- private EditText edtnpm,edtnama,edtpassword,edtprodi;
- private Button btnkirim;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mRequestQueue = Volley.newRequestQueue(this);
- txtdata = (TextView) findViewById(R.id.txtdata);
- edtnpm = (EditText) findViewById(R.id.edtnpm);
- edtnama = (EditText) findViewById(R.id.edtnama);
- edtpassword = (EditText) findViewById(R.id.edtpassword);
- edtprodi = (EditText) findViewById(R.id.edtprodi);
- btnkirim = (Button) findViewById(R.id.btnkirim);
- btnkirim.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String strnpm = edtnpm.getText().toString();
- String strnama = edtnama.getText().toString();
- String strpassword = edtpassword.getText().toString();
- String strprodi = edtprodi.getText().toString();
- if(strnpm.isEmpty()) {
- Toast.makeText(getApplicationContext(),"NPM tidak boleh kosong",
- Toast.LENGTH_LONG).show();
- } else if (strnama.isEmpty()){
- Toast.makeText(getApplicationContext(),"Nama tidak boleh kosong",
- Toast.LENGTH_LONG).show();
- } else if (strpassword.isEmpty()) {
- Toast.makeText(getApplicationContext(),"Password tidak boleh kosong",
- Toast.LENGTH_LONG).show();
- } else if (strprodi.isEmpty()) {
- Toast.makeText(getApplicationContext(),"Prodi tidak boleh kosong",
- Toast.LENGTH_LONG).show();
- } else {
- inputdata(strnpm,strnama,strpassword,strprodi);
- Intent a = new Intent(MainActivity.this, MainActivity.class);
- startActivity(a);
- finish();
- }
- }
- });
- fetchJsonResponse();
- }
- private void fetchJsonResponse() {
- // Pass second argument as "null" for GET requests
- JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, ConfigURL.getAllMhs, null,
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- String result = response.getString("data");
- Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
- JSONArray res = new JSONArray(result);
- for (int i = 0; i < res.length(); i++ ){
- JSONObject jObj = res.getJSONObject(i);
- txtdata.append ("npm = " + jObj.getString("npm") + "\n" +
- "nama = " + jObj.getString("nama") + "\n\n" );
- Log.v("ini data dari server," ,jObj.getString("npm").toString());
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.e("Error: ", error.getMessage());
- }
- });
- /* Add your Requests to the RequestQueue to execute */
- mRequestQueue.add(req);
- }
- //method input data
- private void inputdata(String npm, String nama, String password, String prodi) {
- // final String URL = "/volley/resource/12";
- // Post params to be sent to the server
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("npm", npm);
- params.put("nama", nama);
- params.put("password", password);
- params.put("prodi", prodi);
- JsonObjectRequest req = new JsonObjectRequest(ConfigURL.insertMhs, new JSONObject(params),
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- try {
- VolleyLog.v("Response:%n %s", response.toString(4));
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.e("Error: ", error.getMessage());
- }
- });
- // add the request object to the queue to be executed
- // ApplicationController.getInstance().addToRequestQueue(req);
- mRequestQueue.add(req);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement