Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package com.wina.mongodbnodejs;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. import com.android.volley.Request;
  15. import com.android.volley.RequestQueue;
  16. import com.android.volley.Response;
  17. import com.android.volley.VolleyError;
  18. import com.android.volley.VolleyLog;
  19. import com.android.volley.toolbox.JsonObjectRequest;
  20. import com.android.volley.toolbox.Volley;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.util.HashMap;
  27.  
  28. import server.ConfigURL;
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. private RequestQueue mRequestQueue;
  33. private TextView txtdata;
  34. private EditText edtnpm,edtnama,edtpassword,edtprodi;
  35. private Button btnkirim;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_main);
  41.  
  42. mRequestQueue = Volley.newRequestQueue(this);
  43. txtdata = (TextView) findViewById(R.id.txtdata);
  44.  
  45. edtnpm = (EditText) findViewById(R.id.edtnpm);
  46. edtnama = (EditText) findViewById(R.id.edtnama);
  47. edtpassword = (EditText) findViewById(R.id.edtpassword);
  48. edtprodi = (EditText) findViewById(R.id.edtprodi);
  49.  
  50. btnkirim = (Button) findViewById(R.id.btnkirim);
  51.  
  52. btnkirim.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. String strnpm = edtnpm.getText().toString();
  56. String strnama = edtnama.getText().toString();
  57. String strpassword = edtpassword.getText().toString();
  58. String strprodi = edtprodi.getText().toString();
  59.  
  60. if(strnpm.isEmpty()) {
  61. Toast.makeText(getApplicationContext(),"NPM tidak boleh kosong",
  62. Toast.LENGTH_LONG).show();
  63. } else if (strnama.isEmpty()){
  64. Toast.makeText(getApplicationContext(),"Nama tidak boleh kosong",
  65. Toast.LENGTH_LONG).show();
  66. } else if (strpassword.isEmpty()) {
  67. Toast.makeText(getApplicationContext(),"Password tidak boleh kosong",
  68. Toast.LENGTH_LONG).show();
  69. } else if (strprodi.isEmpty()) {
  70. Toast.makeText(getApplicationContext(),"Prodi tidak boleh kosong",
  71. Toast.LENGTH_LONG).show();
  72. } else {
  73. inputdata(strnpm,strnama,strpassword,strprodi);
  74.  
  75. Intent a = new Intent(MainActivity.this, MainActivity.class);
  76. startActivity(a);
  77. finish();
  78. }
  79. }
  80. });
  81.  
  82. fetchJsonResponse();
  83. }
  84.  
  85. private void fetchJsonResponse() {
  86. // Pass second argument as "null" for GET requests
  87. JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, ConfigURL.getAllMhs, null,
  88. new Response.Listener<JSONObject>() {
  89. @Override
  90. public void onResponse(JSONObject response) {
  91. try {
  92. String result = response.getString("data");
  93. Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
  94.  
  95.  
  96. JSONArray res = new JSONArray(result);
  97. for (int i = 0; i < res.length(); i++ ){
  98. JSONObject jObj = res.getJSONObject(i);
  99. txtdata.append ("npm = " + jObj.getString("npm") + "\n" +
  100. "nama = " + jObj.getString("nama") + "\n\n" );
  101. Log.v("ini data dari server," ,jObj.getString("npm").toString());
  102. }
  103.  
  104. } catch (JSONException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }, new Response.ErrorListener() {
  109. @Override
  110. public void onErrorResponse(VolleyError error) {
  111. VolleyLog.e("Error: ", error.getMessage());
  112. }
  113. });
  114.  
  115. /* Add your Requests to the RequestQueue to execute */
  116. mRequestQueue.add(req);
  117. }
  118.  
  119. //method input data
  120. private void inputdata(String npm, String nama, String password, String prodi) {
  121. // final String URL = "/volley/resource/12";
  122. // Post params to be sent to the server
  123. HashMap<String, String> params = new HashMap<String, String>();
  124. params.put("npm", npm);
  125. params.put("nama", nama);
  126. params.put("password", password);
  127. params.put("prodi", prodi);
  128.  
  129. JsonObjectRequest req = new JsonObjectRequest(ConfigURL.insertMhs, new JSONObject(params),
  130. new Response.Listener<JSONObject>() {
  131. @Override
  132. public void onResponse(JSONObject response) {
  133. try {
  134. VolleyLog.v("Response:%n %s", response.toString(4));
  135. } catch (JSONException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. }, new Response.ErrorListener() {
  140. @Override
  141. public void onErrorResponse(VolleyError error) {
  142. VolleyLog.e("Error: ", error.getMessage());
  143. }
  144. });
  145.  
  146. // add the request object to the queue to be executed
  147. // ApplicationController.getInstance().addToRequestQueue(req);
  148. mRequestQueue.add(req);
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement