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.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.AsyncTask; import android.util.Log; import com.ved.musicmapapp.Objects.Artist; import com.ved.musicmapapp.utils.Statics; public class GetCategoryDetailTask extends AsyncTask<Integer, String, String> { private Activity activity; private ArrayList<String> details; private Artist artist; public GetCategoryDetailTask(Activity activity, Artist artist) { Log.i("check","In GetCategoryDetailTask"); this.activity = activity; this.artist = artist; details = new ArrayList<String>(); } @Override protected String doInBackground(Integer... params) { try { if (!Statics.isNetworkConnected(activity) || null == artist) return null; String ws = ""; switch (params[0]) { case 1: ws = "getBooksByArtist.php"; break; case 2: ws = "getMoviesByArtist.php"; break; case 3: ws = "getSchoolsByArtist.php"; break; case 4: ws = "getGamesByArtist.php"; break; case 5: ws = "getOthersByArtist.php"; break; } 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 + ws); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("name", artist .getName())); 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"); } // publishProgress(sb.toString()); Log.i("RESULT CATAGORY", sb.toString()); if (isCancelled()) return null; JSONArray jArray = new JSONArray(sb.toString().trim()); for (int i = 0; i < jArray.length(); i++) { JSONObject job = jArray.getJSONObject(i); details.add(job.getString("name")); } } } 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(); } catch (JSONException e) { e.printStackTrace(); return e.getMessage(); } return "OK"; } @Override protected void onProgressUpdate(String... values) { // Statics.showToast(activity, values[0]); } @Override protected void onPostExecute(String result) { onSuccess(details); } public void onSuccess(ArrayList<String> details) { } }