Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package com.adstest.app;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  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.toolbox.StringRequest;
  19. import com.android.volley.toolbox.Volley;
  20. import com.bumptech.glide.Glide;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.util.ArrayList;
  27.  
  28. public class Main2Activity extends AppCompatActivity {
  29.  
  30.     ImageView imageView;
  31.     TextView textView, textView2;
  32.     LinearLayout linearLayout;
  33.  
  34.     @Override
  35.     protected void onCreate(Bundle savedInstanceState) {
  36.         super.onCreate(savedInstanceState);
  37.         setContentView(R.layout.activity_main2);
  38.         imageView = findViewById(R.id.imageView);
  39.         textView = findViewById(R.id.txtView);
  40.         textView2 = findViewById(R.id.txtView2);
  41.         linearLayout = findViewById(R.id.linearLayout);
  42.         loadData();
  43.     }
  44.  
  45.     private void loadData() {
  46.         final ProgressDialog progressDialog = new ProgressDialog(Main2Activity.this);
  47.         progressDialog.setMessage("Loading Sedang Mengambil Data...");
  48.         progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  49.         progressDialog.show();
  50.  
  51.         String JSON_URL = "https://api.github.com/search/users?q=eyehunt";
  52.         StringRequest stringRequest = new StringRequest(Request.Method.GET,
  53.                 JSON_URL, new Response.Listener<String>() {
  54.             @Override
  55.             public void onResponse(String s) {
  56.                 try {
  57.                     JSONObject object = new JSONObject(s);
  58.                     final JSONArray array = new JSONArray(object.getString("items"));
  59.                     for (int i = 0; i < array.length(); i++) {
  60.                         Glide.with(Main2Activity.this).load(array.getJSONObject(i)
  61.                                 .get("avatar_url")).into(imageView);
  62.                         textView.setText("Name: "+ array.getJSONObject(i).get("login").toString());
  63.                         textView2.setText("Score: " +array.getJSONObject(i).get("score").toString());
  64.  
  65.                         final String url = array.getJSONObject(i).get("url").toString();
  66.                         linearLayout.setOnClickListener(new View.OnClickListener() {
  67.                             @Override
  68.                             public void onClick(View view) {
  69.                                 Intent intent = new Intent(Intent.ACTION_VIEW);
  70.                                 intent.setData(Uri.parse(url));
  71.                                 startActivity(intent);
  72.                             }
  73.                         });
  74.  
  75.                     }
  76.  
  77.                 } catch (JSONException e) {
  78.                     e.printStackTrace();
  79.                 }
  80.                 progressDialog.dismiss();
  81.             }
  82.         }, new Response.ErrorListener() {
  83.             @Override
  84.             public void onErrorResponse(VolleyError volleyError) {
  85.                 Toast.makeText(Main2Activity.this, "Some error occurred", Toast.LENGTH_LONG).show();
  86.             }
  87.         });
  88.         RequestQueue requestQueue = Volley.newRequestQueue(Main2Activity.this.getApplicationContext());
  89.         requestQueue.add(stringRequest);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement