package com.ved.musicmapapp.providers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import com.ved.musicmapapp.utils.Statics;
public class RenamePlaylistTask extends AsyncTask<Void, Void, String> {
private Activity act;
private ProgressDialog dialog;
private int _playlistId;
private String _newName;
public RenamePlaylistTask(Activity act, int playlistId, String newName) {
Log.i("check","In RenamePlaylistTask");
this.act = act;
this._playlistId = playlistId;
this.dialog = new ProgressDialog(act);
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this._newName = newName;
}
@Override
protected String doInBackground(Void... params) {
String res = null;
try{
if(!Statics.isNetworkConnected(act)){
return "INTERNET CONNECTION ISSUE";
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Statics.WS_PATH + "renamePlayList.php");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("playlist_id", _playlistId+""));
nvps.add(new BasicNameValuePair("user_id", Statics.curUser.getID()+""));
nvps.add(new BasicNameValuePair("playlist_name", _newName));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
String line;
StringBuilder sb = new StringBuilder("");
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
res = sb.toString();
JSONObject obj = new JSONObject(res);
res = obj.getString("message");
}
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return res;
}
@Override
protected void onPostExecute(String result) {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
}