Guest User

MainActivity

a guest
Apr 10th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package com.ilb.deds3c.fetch;
  2.  
  3. import android.app.Activity;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.app.ProgressDialog;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.android.volley.RequestQueue;
  14. import com.android.volley.Response;
  15. import com.android.volley.VolleyError;
  16. import com.android.volley.toolbox.StringRequest;
  17. import com.android.volley.toolbox.Volley;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  24.  
  25. private EditText id;
  26. private Button buttonGet;
  27. private TextView textViewResult;
  28.  
  29. private ProgressDialog loading;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35.  
  36. id = (EditText) findViewById(R.id.editTextId);
  37. buttonGet = (Button) findViewById(R.id.buttonGet);
  38. textViewResult = (TextView) findViewById(R.id.textViewResult);
  39.  
  40. buttonGet.setOnClickListener(this);
  41. }
  42.  
  43. private void getData() {
  44. String id1 = id.getText().toString().trim();
  45. if (id1.equals("")) {
  46. Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
  47. return;
  48. }
  49. loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
  50.  
  51. String url = Config.DATA_URL+id.getText().toString().trim();
  52.  
  53. StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
  54.  
  55. @Override
  56. public void onResponse(String response) {
  57. loading.dismiss();
  58. showJSON(response);
  59. }
  60. },
  61. new Response.ErrorListener() {
  62. @Override
  63. public void onErrorResponse(VolleyError error) {
  64. Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
  65. }
  66. });
  67.  
  68. RequestQueue requestQueue = Volley.newRequestQueue(this);
  69. requestQueue.add(stringRequest);
  70. }
  71.  
  72. private void showJSON(String response){
  73. String name = "";
  74. String phone = "";
  75. String username = "";
  76. String email = "";
  77. String password = "";
  78. try {
  79. JSONObject jsonObject = new JSONObject(response);
  80. JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
  81. JSONObject c = result.getJSONObject(0);
  82.  
  83. name = c.getString(Config.KEY_NAME);
  84. phone = c.getString(Config.KEY_PHONE);
  85. username = c.getString(Config.KEY_USERNAME);
  86. email = c.getString(Config.KEY_EMAIL);
  87. password = c.getString(Config.KEY_PASSWORD);
  88.  
  89. } catch (JSONException e) {
  90. e.printStackTrace();
  91. }
  92. textViewResult.setText("Name:\t"+name+"\nPhone:\t"+phone+"\nUsername:\t"+username+"\nEmail:\t"+email+"\nPassword:\t"+password);
  93. //textViewResult.setText(name);
  94. //textViewResult.setText(phone);
  95. //textViewResult.setText(username);
  96. //textViewResult.setText(email);
  97. //textViewResult.setText(password);
  98. }
  99.  
  100. @Override
  101. public void onClick(View v) {
  102. getData();
  103. }
  104. }
Add Comment
Please, Sign In to add comment