Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class AsyncFetchDetailSiswa extends AsyncTask<String, String, String> {
- ProgressDialog pdLoading = new ProgressDialog(DetailSiswa.this);
- HttpURLConnection conn;
- URL url = null;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- //this method will be running on UI thread
- pdLoading.setMessage("\tLoading...");
- pdLoading.setCancelable(false);
- pdLoading.show();
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- // Enter URL address where your json file resides
- // Even you can make call to php file which returns json data
- url = new URL(baseUrl+"detailssiswa/"+nik);
- Log.d("url siswa nik",String.valueOf(url));
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return e.toString();
- }
- try {
- // Setup HttpURLConnection class to send and receive data from php and mysql
- conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(READ_TIMEOUT);
- conn.setConnectTimeout(CONNECTION_TIMEOUT);
- conn.setUseCaches(false);
- conn.setDoOutput(false);
- conn.setDoInput(true);
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("charset", "utf-8");
- conn.setRequestMethod("GET");
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- return e1.toString();
- }
- try {
- int response_code = conn.getResponseCode();
- Log.d("Response detail siswa", String.valueOf(conn.getResponseCode()));
- // Check if successful connection made
- if (response_code == HttpURLConnection.HTTP_OK) {
- // Read data sent from server
- InputStream input = conn.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- // Pass data to onPostExecute method
- return (result.toString());
- } else {
- return (String.valueOf(response_code));
- }
- } catch (IOException e) {
- e.printStackTrace();
- return e.toString();
- } finally {
- conn.disconnect();
- }
- }
- @Override
- protected void onPostExecute(String result) {
- //this method will be running on UI thread
- try {
- JSONObject json = new JSONObject(result).getJSONObject("data");
- strnik = json.getString("nik");
- strnama = json.getString("nama_siswa");
- strkelas = json.getString("id_kelas_siswa");
- strtmpt_lahir = json.getString("tempat_lahir");
- strtgl_lahir = json.getString("tgl_lahir");
- strjeniskel = json.getString("jenis_kelamin");
- stragama = json.getString("agama");
- stralamat = json.getString("alamat");
- strnotelp = json.getString("no_telp");
- stremail = json.getString("email");
- strImagesiswa = json.getString("foto");
- tvnik.setText(strnik);
- tvnama.setText(strnama);
- tvtmpt_lahir.setText(strtmpt_lahir);
- tvtgl_lahir.setText(strtgl_lahir);
- tvkelas.setText(strkelas);
- tvjeniskel.setText(strjeniskel);
- tvagama.setText(stragama);
- tvalamat.setText(stralamat);
- tvnotelp.setText(strnotelp);
- tvemail.setText(stremail);
- // load image into imageview using glide
- // Glide.with(getApplicationContext()).load(strImagesiswa)
- // .placeholder(R.drawable.man)
- // .error(R.drawable.man)
- // .dontTransform()
- // .diskCacheStrategy(DiskCacheStrategy.ALL)
- // .into(ivDetailSiswa);
- Glide
- .with(getApplicationContext())
- .load(strImagesiswa)
- .asBitmap()
- .into(new SimpleTarget<Bitmap>(100,100) {
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
- ivDetailSiswa.setImageBitmap(resource); // Possibly runOnUiThread()
- pdLoading.dismiss();
- }
- });
- } catch (JSONException e) {
- Toast.makeText(DetailSiswa.this, e.toString()+result, Toast.LENGTH_LONG).show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement