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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import com.ved.musicmapapp.utils.Statics;
public class UpdateLocationTask extends AsyncTask<Void, Void, Integer> {
String error;
private Context context;
private double lat, lon;
private String userId;
private UpdateLocationListener mUpdatedListener;
public UpdateLocationTask(Context context, double lat, double lon,
UpdateLocationListener listener) {
Log.i("check","In UpdateLocationTask");
this.context = context;
this.lat = lat;
this.lon = lon;
mUpdatedListener = listener;
SharedPreferences prefs = context.getSharedPreferences("MUSIC_MAP",
Context.MODE_PRIVATE);
userId = prefs.getString("FB_ID", null);
}
@Override
protected Integer doInBackground(Void... params) {
try {
if (!Statics.isNetworkConnected(context) || userId == null)
return 0;
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(Statics.WS_PATH
+ "updateLocation.php");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fbid", userId));
nvps.add(new BasicNameValuePair("lat", lat + ""));
nvps.add(new BasicNameValuePair("long", lon + ""));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
// get the response content as a string
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");
}
JSONObject jsonObject = new JSONObject(sb.toString());
error = jsonObject.getString("message");
return jsonObject.getInt("success");
}
} catch (UnsupportedEncodingException e1) {
Log.e("Exception", "" + e1.getMessage());
error = e1.getMessage();
// e1.printStackTrace();
} catch (ClientProtocolException e1) {
Log.e("ClientProtocolException", "" + e1.getMessage());
error = e1.getMessage();
// e1.printStackTrace();
} catch (IOException e1) {
Log.e("IOException", "" + e1.getMessage());
error = e1.getMessage();
e1.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
error = e.getMessage();
} catch (Exception e) {
e.printStackTrace();
error = e.getMessage();
}
return 0;
}
@Override
protected void onPostExecute(Integer result) {
Log.i("check","End UPdate Location Task");
if (result == 1 && null != userId) {
if (mUpdatedListener != null) {
mUpdatedListener.onUpdateComplete(true);
}
} else {
if (mUpdatedListener != null) {
mUpdatedListener.onUpdateComplete(false);
}
}
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
public interface UpdateLocationListener {
public void onUpdateComplete(boolean isSuccess);
}
}