Back to project page Itune.
The source code is released under:
MIT License
If you think the Android project Itune listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.ripoff.itune; //from ww w . ja v a 2s . c o m import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.GINGERBREAD) public class HttpFetchActivity extends ActionBarActivity { public final static String EXTRA_URL = "com.ripoff.itune.URL"; public static final String URL_ITUNE = "http://itunes.apple.com/search?"; public final static String GET_LIMIT = "&limit="; public final static String GET_TERM="term="; public final static String TAG_ANAME="artistName"; public final static String TAG_TNAME="trackName"; public final static String TAG_URL30="artworkUrl30"; private final static int K = 2; public JSONArray results = null; public ListView listView; public ArrayList<String> arrlist = new ArrayList<String>(); public ArrayList<Bitmap> bitmapCache = new ArrayList<Bitmap>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_http_fetch); //Permit execution on the main thread StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); //Receive the intent Intent intent = getIntent(); //intent key values final String iRequest = intent.getStringExtra(MainActivity.KEY_URL).replaceAll("\\s",""); /*Remove all white spaces as precaution*/ final String iLimit = intent.getStringExtra(MainActivity.KEY_LIMIT); //Establish connection to URL try { URL url = new URL(iRequest); HttpURLConnection con = (HttpURLConnection) url.openConnection(); readStream(con.getInputStream()); } catch (Exception e) { e.printStackTrace(); } /*-----------Custom Adapter for ListView------------------*/ CustomAdapter newAdapter = new CustomAdapter(); ListView listView= (ListView)findViewById(R.id.list1); listView.setAdapter(newAdapter); /*-----Action to perform when user clicks on a specific entity of the list------*/ listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // get artist name of selected list item String artistName = getArtistName(position); //Construct the URL for searching for the artist name alone //Note that the limit is obtained from the previous intent String url = URL_ITUNE + GET_TERM + artistName + GET_LIMIT + iLimit; // Launching itself (same activity) as the new intent to search for artist Intent i = new Intent(getApplicationContext(), HttpFetchActivity.class); i.putExtra(MainActivity.KEY_URL, url); i.putExtra(MainActivity.KEY_LIMIT,iLimit); startActivity(i); } }); } //Function to get an artist name from 'arrlist' private String getArtistName(int position) { List<String> recordList = arrlist; return recordList.get(position*K); } /* Read the text stream (JSON Object) obtained through URL call */ private void readStream(InputStream in) { BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(in)); //String builder to grow the sting(s) read StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(""); //String to hold buffer state String line=""; //Read line by line while ((line = reader.readLine()) != null) { stringBuilder.append(line); } //Final entire string String finalstring = stringBuilder.toString(); //Parse the JSON string and update 'arrlist' with the list of artists,tracks and bitmaps parseJson(finalstring); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /* Decode the JSON object and obtain specific tag values*/ private void parseJson(String Jstr) { if(Jstr != null){ try{ //Obtain the JSON array under the results tag. JSONObject Jobj = new JSONObject(Jstr); results = Jobj.getJSONArray("results"); for(int i=0; i<results.length(); i++){ //i'th object element of the JSON array JSONObject Jtemp = results.getJSONObject(i); //Obtain Value of specific tags String Art_name = Jtemp.getString(TAG_ANAME); String Track_name = Jtemp.getString(TAG_TNAME); String url30 = Jtemp.getString(TAG_URL30); //Add the values to 'arrlist' arrlist.add(Art_name); arrlist.add(Track_name); //Download the bitmaps and store it in a cache bitmapCache.add(download_Image(url30)); } Log.w("Itune-query","JSON parse Complete!"); }catch(JSONException e){ e.printStackTrace(); } } } // Custom ListView adapter public class CustomAdapter extends BaseAdapter{ List<String> recordList = arrlist; @Override public int getCount() { // TODO Auto-generated method stub /*Treat the list as a list as list of records with K items per entity*/ return recordList.size()/K; } @Override public String getItem(int arg0) { // TODO Auto-generated method stub return recordList.get(arg0); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub if(arg1 == null){ LayoutInflater inflater = LayoutInflater.from(arg2.getContext()); arg1 = inflater.inflate(R.layout.listitem ,arg2,false); } //View slots for Artist, track and icon TextView artistName = (TextView)arg1.findViewById(R.id.textView1); TextView trackName = (TextView)arg1.findViewById(R.id.textView2); ImageView icon = (ImageView)arg1.findViewById(R.id.imageView1); //Retrieve the info to display on one list entity String temp_artistName = recordList.get(arg0*K); String temp_trackName = recordList.get(arg0*K+1); //Display the artist name and track name artistName.setText(temp_artistName); trackName.setText(temp_trackName); //Obtain the Bitmap icons from cache and display it icon.setImageBitmap(bitmapCache.get(arg0)); return arg1; } } private Bitmap download_Image(String url) { /* Function to download a bitmap from a given URL - SHAMELESSLY COPIED*/ Bitmap bmp =null; try{ URL ulrn = new URL(url); HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); InputStream is = con.getInputStream(); bmp = BitmapFactory.decodeStream(is); if (null != bmp) return bmp; }catch(Exception e){} return bmp; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.http_fetch, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_http_fetch, container, false); return rootView; } } }