package com.ved.musicmapapp.providers; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.widget.Toast; import com.ved.musicmapapp.R; import com.ved.musicmapapp.Objects.Song; import com.ved.musicmapapp.fragments.DigitalTrackFragment; import com.ved.musicmapapp.utils.XMLParse; public class GetTrackBuyLink extends AsyncTask<Void, Void, String> { String error; ProgressDialog mProgressDialog; Activity context; Song song; public GetTrackBuyLink(Activity context, Song song) { this.context = context; Log.i("check","In GetTrackBuyLink"); this.song = song; mProgressDialog = new ProgressDialog(context); mProgressDialog.setMessage("Loading..."); mProgressDialog.setCancelable(true); mProgressDialog .setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { cancel(true); } }); } @Override protected String doInBackground(Void... params) { try { String countryStr = context.getResources().getConfiguration().locale.getCountry().toLowerCase(); if(!countryStr.equals("us")) countryStr = "gb"; StringBuilder url = new StringBuilder( GetTrackPreview._7DIGITAL_API_LINK); url.append(GetTrackPreview._7DIGITAL_API_TRACK_DETAIL); url.append("?trackid=" + song.get7DigigtalTrackId(countryStr)); url.append("&oauth_consumer_key=" + GetTrackPreview._7DIGITAL_API_KEY); Log.d("REQUEST", url.toString()); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 20000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 20000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); HttpGet httpGet = new HttpGet(url.toString()); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); String xml = EntityUtils.toString(httpEntity); return getBuyLink(xml); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); String countryStr = context.getResources().getConfiguration().locale.getCountry().toLowerCase(); if(!countryStr.equals("us")) countryStr = "gb"; if (result == null) { Toast.makeText(context, "Can't get buy link!", Toast.LENGTH_LONG).show(); song.setBuyLink("",countryStr); } else { try { song.setBuyLink(result,countryStr); DigitalTrackFragment fragment = new DigitalTrackFragment(); fragment.setUrl(result); FragmentTransaction fragmentTransaction = ((FragmentActivity)context) .getSupportFragmentManager() .beginTransaction(); fragmentTransaction.replace(R.id.frame_content, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); // Intent intent = new Intent(Intent.ACTION_VIEW, // Uri.parse(result)); // context.startActivity(intent); // update to Server new SubmitSongBuyLink().execute(song.getID() + "", song.get7DigigtalTrackId(countryStr), result,countryStr); } catch (Exception e) { } } super.onPostExecute(result); } @Override protected void onPreExecute() { mProgressDialog.show(); super.onPreExecute(); } @Override protected void onCancelled() { if (mProgressDialog.isShowing()) mProgressDialog.dismiss(); super.onCancelled(); } private String getBuyLink(String xml) { Document doc = XMLParse.getDomElement(xml); Node result = doc.getFirstChild().getFirstChild(); NodeList resultNodes = result.getChildNodes(); int i = 0; NodeList realeaseNodes = null; while (i < resultNodes.getLength()) { if (resultNodes.item(i).getNodeName().equals("release")) { realeaseNodes = resultNodes.item(i).getChildNodes(); break; } i++; } String link = null; if (realeaseNodes != null) { i = 0; while (i < realeaseNodes.getLength()) { if (realeaseNodes.item(i).getNodeName().equals("url")) { link = realeaseNodes.item(i).getTextContent(); break; } i++; } } return link; } }