package com.example.asynctask01;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 

//http://www.journaldev.com/9708/android-asynctask-example-tutorial

//http://programmerguru.com/android-tutorial/android-asynctask-example/

//http://javatechig.com/video/json-feed-reader-in-android
	
//http://codeoncloud.blogspot.fr/2013/07/android-mysql-php-json-tutorial.html


 
// handler ????
// JSON objet

public class MainActivity extends Activity
{
    private Button button;
    private EditText time;
    private TextView finalResult;
    String plouf;
 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        
	        time = (EditText) findViewById(R.id.in_time);
	        button = (Button) findViewById(R.id.btn_run);
	        finalResult = (TextView) findViewById(R.id.tv_result);
        
	        button.setOnClickListener(new View.OnClickListener()
	        {
		            @Override
		            public void onClick(View v) 
		            {
		                AsyncTaskRunner runner = new AsyncTaskRunner();
		                //new TCP().execute("http://www.analog-design.net/MP3/listing.php");
		                runner.execute("http://www.analog-design.net/MP3/listing.php");
		                Toast.makeText(MainActivity.this, plouf, Toast.LENGTH_LONG).show();
		            }
	        });
    }
 
    private class AsyncTaskRunner extends AsyncTask<String, String, String> 
    {
 
        @Override
        protected String doInBackground(String... urls) 
        {
	        	InputStream inputStream = null;
	            String result = "";
	
	            try
	            {
	     
	                HttpClient httpclient = new DefaultHttpClient();
	                HttpResponse httpResponse = httpclient.execute(new HttpGet(urls[0]));
	                inputStream = httpResponse.getEntity().getContent();
	                BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
	                String line = "";
	               
	                while((line = bufferedReader.readLine()) != null)
	                    result += line;
	         
	                inputStream.close();
	     
	            } 
	            
	            catch (Exception e) 
	            {
	                Log.d("InputStream", e.getLocalizedMessage());
	            }
	     
	            return result;
        }
 
        @Override
        protected void onPostExecute(String result)
        {
            Log.d("InputStream", result);
            plouf = result;
        }
        
       
    }
}
