Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. package com.example.romain.lastfmtopartists;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo;
  7. import android.os.AsyncTask;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.ListAdapter;
  15. import android.widget.ListView;
  16. import android.widget.SimpleAdapter;
  17. //import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.HashMap;
  26.  
  27. public class ViewFM extends AppCompatActivity {
  28.     private String TAG = ViewFM.class.getSimpleName();
  29.     public static final String  urlString = "http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=belgium&api_key=32ef5df0e36797b605e205529058f3b8&format=json&limit=";
  30.     private String toSend = urlString;
  31.     //private ModelFM model;
  32.     private Button but;
  33.     private EditText editTxt;
  34.     // barre de progression du téléchargement
  35.     private ProgressDialog dialog;
  36.     private ListView lv;
  37.     // contient les différents artistes et le nombre d'écoutes
  38.     private ArrayList<HashMap<String,String>> artistList;
  39.  
  40.     @Override
  41.     protected void onCreate(Bundle savedInstanceState) {
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.activity_main);
  44.         //model = ((Builder) getApplication()).getModel();
  45.         lv = (ListView) findViewById(R.id.list);
  46.         artistList = new ArrayList<>();
  47.         initOnclick();
  48.     }
  49.  
  50.     /**** Method who initialize the onClicks ****/
  51.     public void initOnclick(){
  52.         but = (Button) findViewById(R.id.button);
  53.         but.setOnClickListener(new View.OnClickListener() {
  54.             @Override
  55.             public void onClick(View view) {
  56.                 ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  57.                 NetworkInfo nInfo = connMgr.getActiveNetworkInfo();
  58.  
  59.                 if(nInfo != null || nInfo.isConnected()){
  60.                     /** connexion ok  **/
  61.                     but.setEnabled(true);
  62.                     editTxt = (EditText) findViewById(R.id.editText);
  63.                     String input = editTxt.getText().toString();
  64.                     toSend+=input;
  65.                     Log.e(TAG,toSend);
  66.                     new DownLoadLastFm().execute(toSend);
  67.                     toSend = urlString;
  68.                 }else {
  69.                     //
  70.                     /** connexion ko**/
  71.                     but.setEnabled(false);
  72.                     Context context = getApplicationContext();
  73.                     CharSequence text = nInfo.getReason();
  74.                     //todo print error in a toast, que renvoie getReason?
  75.                     int duration = Toast.LENGTH_SHORT;
  76.                     Toast toast = Toast.makeText(context, text, duration);
  77.                     toast.show();
  78.                 }
  79.             }
  80.         });
  81.     }
  82.  
  83.     /**** Inner class who is in charge for download the data ****/
  84.     private class DownLoadLastFm extends AsyncTask<String, Void,Void>{
  85.         @Override
  86.         protected void onPreExecute() {
  87.             super.onPreExecute();
  88.             // on affiche la barre de progression
  89.             dialog = new ProgressDialog(ViewFM.this);
  90.             dialog.setMessage("Patientez svp ...");
  91.             dialog.setCancelable(false);
  92.             dialog.show();
  93.         }
  94.  
  95.         @Override
  96.         protected Void doInBackground(String... urls) {
  97.             //todo passer l'url en param
  98.             HttpHandler httpH = new HttpHandler();
  99.             Log.e(TAG,urls[1]);
  100.             String jsonStr = httpH.makeHttpRequest(urls[1]);
  101.  
  102.             Log.e(TAG,"Response from url "+jsonStr);
  103.  
  104.             if(jsonStr != null){
  105.                 try {
  106.                     JSONObject jsonObject = new JSONObject(jsonStr).getJSONObject("topartists");
  107.                     JSONArray artists = jsonObject.getJSONArray("artist");
  108.                     Log.e(TAG,"contenu de l'array"+artists.toString());
  109.                     for (int i=0;i<artists.length();i++){
  110.                         JSONObject jo = artists.getJSONObject(i);
  111.                         String name = jo.getString("name");
  112.                         String listeners = jo.getString("listeners");
  113.  
  114.                         HashMap<String,String> tmp = new HashMap<>();
  115.                         tmp.put("name", name);
  116.                         tmp.put("listeners", listeners);
  117.  
  118.                         artistList.add(tmp);
  119.                     }
  120.                 } catch (final JSONException e){
  121.                     Log.e(TAG, "Json parsing error: " + e.getStackTrace());
  122.                     runOnUiThread(new Runnable() {
  123.                         @Override
  124.                         public void run() {
  125.                             Toast.makeText(getApplicationContext(),
  126.                                     "Json parsing error: " + e.getMessage(),
  127.                                     Toast.LENGTH_LONG)
  128.                                     .show();
  129.                         }
  130.                     });
  131.                 }
  132.             } else {
  133.                 Log.e(TAG, "Couldn't get json from server.");
  134.                 runOnUiThread(new Runnable() {
  135.                     @Override
  136.                     public void run() {
  137.                         Toast.makeText(getApplicationContext(),
  138.                                 "Couldn't get json from server. Check LogCat for possible errors!",
  139.                                 Toast.LENGTH_LONG)
  140.                                 .show();
  141.                     }
  142.                 });
  143.             }
  144.             return null;
  145.         }
  146.  
  147.         @Override
  148.         protected void onPostExecute(Void s) {
  149.             super.onPostExecute(s);
  150.             // on cache la barre
  151.             if(dialog.isShowing()){
  152.                 dialog.dismiss();
  153.             }
  154.             /** On met à jour les données json parsées avec un adapteur **/
  155.             ListAdapter adapter = new SimpleAdapter(ViewFM.this,artistList,R.layout.list_item,
  156.                     new String[]{"name","listeners"}, new int[]{R.id.name, R.id.listeners});
  157.             lv.setAdapter(adapter);
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement