package in.nfluence.nfluencemovies;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.GridLayout;
import android.widget.RelativeLayout;
/**
* Created by ${ajinkya} on ${2016-04-04}.
*/
public class Hotstar extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setContentView(R.layout.movies_main);
new LoadMovieData(Hotstar.this).execute("http://192.168.1.8/nFluenceMovies/database/Movies.php");
}
private class LoadMovieData extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
public Hotstar activity;
public LoadMovieData(Hotstar a)
{
this.activity = a;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ImageView seticon=(ImageView)findViewById(R.id.setIcon);
seticon.setImageResource(R.drawable.hotstar);
TextView heading=(TextView)findViewById(R.id.movietv);
heading.setTextColor(Color.parseColor("#5ccf09"));
heading.setText("Hotstar");
pDialog = new ProgressDialog((Hotstar.this));
pDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Large);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
//Data will be sent to the server as a list of KeyValue pairs.
String url=params[0];
Log.d("MainActivity", "before try:");
try {
Log.d("MainActivity", "in try:");
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(0);
nameValuePairs.add(new BasicNameValuePair("category","Hotstar"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
Log.d("MainActivity", "Toast:" + response);
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
try {
JSONObject json = new JSONObject(result);
JSONArray categories;
categories = json.getJSONArray("hotstar");
GridLayout layout = (GridLayout)findViewById(R.id.gridmovie);
//Movies
for (int i = 0; i < categories.length(); i++) {
final JSONObject catObj = (JSONObject) categories.get(i);
LinearLayout linearLayout = new LinearLayout(Hotstar.this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
300,
LinearLayout.LayoutParams.WRAP_CONTENT);
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
linearLayout.setBackground(getResources().getDrawable(R.drawable.border));
}*/
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);
layout.addView(linearLayout);
ImageView imageButton = new ImageView(Hotstar.this);
LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(250,250);
lpImage.gravity=Gravity.CENTER;
//Setting the parameters on the Image
imageButton.setLayoutParams(lpImage);
if(!(catObj.get("link").toString().isEmpty())){
Bitmap bitmap = loadImage(catObj.get("link").toString());
imageButton.setImageBitmap(bitmap);
imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
try {
intent.setData(Uri.parse(catObj.get("redirectlink").toString()));
} catch (JSONException e) {
e.printStackTrace();
}
startActivity(intent);
}
});
}
//adding imageview to relative layout
linearLayout.addView(imageButton);
TextView textview = new TextView(Hotstar.this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
// textview.setBackgroundResource(android.R.color.holo_red_light);
textview.setText(catObj.get("name").toString());
// textview.setTextSize(50);
//textview.setPadding(60,0,0,0);
textview.bringToFront();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textview.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
//Adding TextView to relative layout
linearLayout.addView(textview);
// layout.addView(name);
Log.d("MAINLINKS:", catObj.get("link").toString());
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
protected Bitmap loadImage(String utl2) {
// TODO Auto-generated method stub
Log.v("utl2--", utl2);
URL imageURL = null;
Bitmap bitmap = null;
try {
imageURL = new URL(utl2);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection) imageURL
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Hi,
Thanks for the info, But How do I get to know the data which I have is also available on Hotstar?
And how did you get the data to link it to Hotstar.?
is there any json API from Hotstar?
Hi bishwajith bn,
This post is about to ‘BUILD’ app like HOTSTAR. It is not related to get direct data from hotstar. I have explained only how to build movies category app like Hotstar, Sonyliv- LIVE, OZEE etc just to clear concept of dynamic layout and JSON parsing. If you want to show data from Hotstar app then you have to contact developers of Hotstar. Some apps do the same process. For example , OZEE app gets data from Ditto TV app. If you want to build something big or do business from your app then only contact to Hotstar.
Thanks,
Ajinkya