package com.ved.musicmapapp.providers; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collections; 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.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.util.Log; import com.ved.musicmapapp.Objects.User; import com.ved.musicmapapp.utils.Statics; import com.ved.musicmapapp.utils.UserSort; public class GetUserByTypeTask extends AsyncTask<Void, ArrayList<User>, String> { public static final int YOU_FOLLOW = 0; public static final int FOLLOW_YOU = 1; public static final int SIMILAR_ME = 2; public static final int ARTIS_BELONG_TO_USER = 3; public static final String YOU_FOLLOW_SP = "you_follow"; public static final String FOLLOW_YOU_SP = "follow_you"; public static final String SIMILAR_ME_SP = "similar_me"; public static final String ARTIS_BELONG_TO_USER_SP = "artis_belong_to_user"; private Context mContext; private int mType; private String mFbID; private GetDataListener mListener; private ArrayList<User> users; private SharedPreferences mSharedPreferences; private String mKey; public GetUserByTypeTask(Context context, String fbId, int type) { Log.i("check","In GetUserByTypeTask"); mContext = context; mFbID = fbId; mType = type; mSharedPreferences = mContext.getSharedPreferences("DATA", Context.MODE_PRIVATE); switch (type) { case YOU_FOLLOW: mKey = YOU_FOLLOW_SP; break; case FOLLOW_YOU: mKey = FOLLOW_YOU_SP; break; case SIMILAR_ME: mKey = SIMILAR_ME_SP; break; case ARTIS_BELONG_TO_USER: mKey = ARTIS_BELONG_TO_USER_SP; break; default: break; } } public GetUserByTypeTask(Context context, String fbId, int type, GetDataListener listener) { mContext = context; Log.i("check","In GetUserByTypeTask"); if (mContext != null) { mContext = context; mFbID = fbId; mType = type; mSharedPreferences = mContext.getSharedPreferences("DATA", Context.MODE_PRIVATE); mListener = listener; switch (type) { case YOU_FOLLOW: mKey = YOU_FOLLOW_SP; break; case FOLLOW_YOU: mKey = FOLLOW_YOU_SP; break; case SIMILAR_ME: mKey = SIMILAR_ME_SP; break; case ARTIS_BELONG_TO_USER: mKey = ARTIS_BELONG_TO_USER_SP; break; default: break; } } } @SuppressWarnings("unchecked") @Override protected String doInBackground(Void... params) { if (mContext != null) { try { if (!Statics.isNetworkConnected(mContext)) return null; HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(Statics.WS_PATH+ "getUserByType.php"); HttpContext localContext = new BasicHttpContext(); // httpPost.setHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded;charset=UTF-8"); System.out.println("final url : "+Statics.WS_PATH+ "getUserByType.php"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("fbid", mFbID)); nvps.add(new BasicNameValuePair("type", String.valueOf(mType))); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); HttpResponse response = httpClient.execute(httpPost,localContext); String data = EntityUtils.toString(response.getEntity(), HTTP.UTF_8); /*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 + "getUserByType.php"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("fbid", mFbID)); nvps.add(new BasicNameValuePair("type", String.valueOf(mType))); httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); if (isCancelled() || ((Activity) mContext).isFinishing()) return null; HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity, "UTF-8");*/ users = Statics.getUsersFromJSON(data); if (users != null) { Collections.sort(users, new UserSort()); } publishProgress(users); } catch (UnsupportedEncodingException e1) { Log.e("Exception", "" + e1.getMessage()); return e1.getMessage(); // e1.printStackTrace(); } catch (ClientProtocolException e1) { Log.e("ClientProtocolException", "" + e1.getMessage()); return e1.getMessage(); // e1.printStackTrace(); } catch (IOException e1) { Log.e("IOException", "" + e1.getMessage()); e1.printStackTrace(); return e1.getMessage(); } } return "OK"; } @Override protected void onPostExecute(String result) { if (result != null) { if (users != null && users.size() > 0) { try { for (User user : users) { if (Statics.hashUsers.containsKey(user.getFbid())) { User matchUser = Statics.hashUsers.get(user .getFbid()); switch (mType) { case FOLLOW_YOU: matchUser.setFollowMe(user.isFollowMe()); Statics.isFollowersLoaded = true; break; case YOU_FOLLOW: matchUser.setFollowed(user.isFollowed()); Statics.isFollowingLoaded = true; break; case SIMILAR_ME: Statics.isWhoToFollowLoaded = true; break; default: break; } } else { // Statics.hashUsers.put(user.getFbid(), user); User userTmp = Statics.hashUsers .get(user.getFbid()); if (userTmp != null) { userTmp.compareTo(user); Statics.hashUsers.put(user.getFbid(), userTmp); // DBAdapter adapter = new DBAdapter(mContext); // adapter.addUser(userTmp); } else { Statics.hashUsers.put(user.getFbid(), user); // DBAdapter adapter = new DBAdapter(mContext); // adapter.addUser(userTmp); } } } } catch (Exception ex) { } } switch (mType) { case FOLLOW_YOU: break; case YOU_FOLLOW: break; default: break; } } else { if (mListener != null) mListener.onError(result); } super.onPostExecute(result); } @Override protected void onPreExecute() { users = new ArrayList<User>(); if (mListener != null) { mListener.onStartGetData(); } super.onPreExecute(); } @Override protected void onProgressUpdate(ArrayList<User>... values) { if (mListener != null) { ArrayList<User> mUser = new ArrayList<User>(); for (User user : values[0]) { User userTmp = Statics.hashUsers.get(user.getFbid()); Log.d("Men123", "GotData2131231->" + user.getArtistcount()); if (userTmp != null) { if (user.getArtists() != null && (user.getArtists().size() > 0 || user .getArtists().size() == 0 && user.getNoSong() == 0)) { } else { user.setArtists(userTmp.getArtists()); user.setGenre(userTmp.getGenre()); } userTmp.compareTo(user); Statics.hashUsers.put(user.getFbid(), userTmp); mUser.add(userTmp); Log.d("Men123", "GotData213->" + user.getArtistcount() + ";" + userTmp.getArtistcount()); } else { mUser.add(user); Statics.hashUsers.put(user.getFbid(), userTmp); Log.d("Men1234", "GotData213->" + user.getArtistcount()); } } mListener.onGotData(mUser); } super.onProgressUpdate(values); } public interface GetDataListener { public void onStartGetData(); public void onGotData(ArrayList<User> users); public void onError(String error); } }