Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. private class AsyncFetchDetailSiswa extends AsyncTask<String, String, String> {
  2. ProgressDialog pdLoading = new ProgressDialog(DetailSiswa.this);
  3. HttpURLConnection conn;
  4. URL url = null;
  5.  
  6. @Override
  7. protected void onPreExecute() {
  8. super.onPreExecute();
  9.  
  10. //this method will be running on UI thread
  11. pdLoading.setMessage("\tLoading...");
  12. pdLoading.setCancelable(false);
  13. pdLoading.show();
  14.  
  15. }
  16.  
  17. @Override
  18. protected String doInBackground(String... params) {
  19. try {
  20.  
  21. // Enter URL address where your json file resides
  22. // Even you can make call to php file which returns json data
  23. url = new URL(baseUrl+"detailssiswa/"+nik);
  24. Log.d("url siswa nik",String.valueOf(url));
  25.  
  26. } catch (MalformedURLException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. return e.toString();
  30. }
  31. try {
  32.  
  33. // Setup HttpURLConnection class to send and receive data from php and mysql
  34. conn = (HttpURLConnection) url.openConnection();
  35. conn.setReadTimeout(READ_TIMEOUT);
  36. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  37. conn.setUseCaches(false);
  38. conn.setDoOutput(false);
  39. conn.setDoInput(true);
  40. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  41. conn.setRequestProperty("charset", "utf-8");
  42. conn.setRequestMethod("GET");
  43.  
  44. } catch (IOException e1) {
  45. // TODO Auto-generated catch block
  46. e1.printStackTrace();
  47. return e1.toString();
  48. }
  49.  
  50. try {
  51.  
  52. int response_code = conn.getResponseCode();
  53. Log.d("Response detail siswa", String.valueOf(conn.getResponseCode()));
  54.  
  55.  
  56. // Check if successful connection made
  57. if (response_code == HttpURLConnection.HTTP_OK) {
  58.  
  59. // Read data sent from server
  60. InputStream input = conn.getInputStream();
  61. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  62. StringBuilder result = new StringBuilder();
  63. String line;
  64.  
  65. while ((line = reader.readLine()) != null) {
  66. result.append(line);
  67. }
  68.  
  69. // Pass data to onPostExecute method
  70. return (result.toString());
  71.  
  72. } else {
  73.  
  74. return (String.valueOf(response_code));
  75. }
  76.  
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. return e.toString();
  80. } finally {
  81. conn.disconnect();
  82. }
  83. }
  84.  
  85. @Override
  86. protected void onPostExecute(String result) {
  87.  
  88. //this method will be running on UI thread
  89.  
  90.  
  91. try {
  92.  
  93. JSONObject json = new JSONObject(result).getJSONObject("data");
  94. strnik = json.getString("nik");
  95. strnama = json.getString("nama_siswa");
  96. strkelas = json.getString("id_kelas_siswa");
  97. strtmpt_lahir = json.getString("tempat_lahir");
  98. strtgl_lahir = json.getString("tgl_lahir");
  99. strjeniskel = json.getString("jenis_kelamin");
  100. stragama = json.getString("agama");
  101. stralamat = json.getString("alamat");
  102. strnotelp = json.getString("no_telp");
  103. stremail = json.getString("email");
  104. strImagesiswa = json.getString("foto");
  105.  
  106.  
  107. tvnik.setText(strnik);
  108. tvnama.setText(strnama);
  109. tvtmpt_lahir.setText(strtmpt_lahir);
  110. tvtgl_lahir.setText(strtgl_lahir);
  111. tvkelas.setText(strkelas);
  112. tvjeniskel.setText(strjeniskel);
  113. tvagama.setText(stragama);
  114. tvalamat.setText(stralamat);
  115. tvnotelp.setText(strnotelp);
  116. tvemail.setText(stremail);
  117.  
  118. // load image into imageview using glide
  119. // Glide.with(getApplicationContext()).load(strImagesiswa)
  120. // .placeholder(R.drawable.man)
  121. // .error(R.drawable.man)
  122. // .dontTransform()
  123. // .diskCacheStrategy(DiskCacheStrategy.ALL)
  124. // .into(ivDetailSiswa);
  125. Glide
  126. .with(getApplicationContext())
  127. .load(strImagesiswa)
  128. .asBitmap()
  129. .into(new SimpleTarget<Bitmap>(100,100) {
  130. @Override
  131. public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
  132. ivDetailSiswa.setImageBitmap(resource); // Possibly runOnUiThread()
  133. pdLoading.dismiss();
  134. }
  135. });
  136.  
  137. } catch (JSONException e) {
  138. Toast.makeText(DetailSiswa.this, e.toString()+result, Toast.LENGTH_LONG).show();
  139. }
  140.  
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement