diff --git a/app/src/main/java/com/chameleon/sustcast/home/LiveFragment.java b/app/src/main/java/com/chameleon/sustcast/home/LiveFragment.java index d114c4e..d45e841 100644 --- a/app/src/main/java/com/chameleon/sustcast/home/LiveFragment.java +++ b/app/src/main/java/com/chameleon/sustcast/home/LiveFragment.java @@ -1,8 +1,12 @@ package com.chameleon.sustcast.home; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -11,9 +15,26 @@ import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; +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.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; /** * Created by HEMAYEET on 1/3/2018. @@ -34,11 +55,94 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, CurrentSong(); return view; } + + class MyAsyncTask extends AsyncTask { + + private ProgressDialog progressDialog = new ProgressDialog(getActivity()); + InputStream inputStream = null; + String result = ""; + + protected void onPreExecute() { + progressDialog.setMessage("Downloading your data..."); + progressDialog.show(); + progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + public void onCancel(DialogInterface arg0) { + MyAsyncTask.this.cancel(true); + } + }); + } + @Override + protected Void doInBackground(String... params) { + + String url_select = "http://103.84.159.230:8000/status-json.xsl"; + + ArrayList param = new ArrayList(); + + try { + // Set up HTTP post + + // HttpClient is more then less deprecated. Need to change to URLConnection + HttpClient httpClient = new DefaultHttpClient(); + + HttpPost httpPost = new HttpPost(url_select); + httpPost.setEntity(new UrlEncodedFormEntity(param)); + HttpResponse httpResponse = httpClient.execute(httpPost); + HttpEntity httpEntity = httpResponse.getEntity(); + + // Read content & Log + inputStream = httpEntity.getContent(); + } catch (UnsupportedEncodingException e1) { + Log.e("UnsupportedEncodExcept", e1.toString()); + e1.printStackTrace(); + } catch (ClientProtocolException e2) { + Log.e("ClientProtocolException", e2.toString()); + e2.printStackTrace(); + } catch (IllegalStateException e3) { + Log.e("IllegalStateException", e3.toString()); + e3.printStackTrace(); + } catch (IOException e4) { + Log.e("IOException", e4.toString()); + e4.printStackTrace(); + } + // Convert response to string using String Builder + try { + BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8), 8); + StringBuilder sBuilder = new StringBuilder(); + + String line = null; + while ((line = bReader.readLine()) != null) { + sBuilder.append(line + "\n"); + } + + inputStream.close(); + result = sBuilder.toString(); + + } catch (Exception e) { + Log.e("StringBuild BufferRead", "Error converting result " + e.toString()); + } + return null; + } // protected Void doInBackground(String... params) + protected void onPostExecute(Void v) { + //parse JSON data + try { + JSONArray jArray = new JSONArray(result); + for(int i=0; i < jArray.length(); i++) { + JSONObject jObject = jArray.getJSONObject(i); + Log.i("TAG", jObject.toString()); + } // End Loop + this.progressDialog.dismiss(); + } catch (JSONException e) { + Log.e("JSONException", "Error: " + e.toString()); + } // catch (JSONException e) + } + + } + public void CurrentSong() { try { - URL url = new URL("http://103.84.159.230:8000/sustcast/currentsong?sid=#"); + URL url = new URL("http://103.84.159.230:8000/currentsong?sid=#"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine;