package com.example.patrice.http01;

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

// http://www.thaicreate.com/java/java-url-send-method-post-get.html

// https://perso.univ-rennes1.fr/pierre.nerzic/Android/semaine7.pdf

// https://ihofmann.wordpress.com/2013/01/23/android-sending-post-requests-with-parameters/

// https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

// https://www.survivingwithandroid.com/2013/05/android-http-downlod-upload-multipart.html

// http://o7planning.org/en/10527/android-networking-tutorial



public class http02 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_02_layout);

        checkInternetConenction();

        URL url;
        try {


            String adresse = "http://analog-design.net/WORKAREA/WEBSITE/Codebar/http02.php";

            URL urlToRequest = new URL(adresse);
            HttpURLConnection urlConnection = (HttpURLConnection) urlToRequest.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

            String postParameters = "name=value1&surname=value2";

            urlConnection.setFixedLengthStreamingMode( postParameters.getBytes().length);
            PrintWriter out = new PrintWriter(urlConnection.getOutputStream());
            out.print(postParameters);
            out.close();


/*
            //url = new URL("http://localhost/myphp/url.php");
            url = new URL(adresse);

            String Name = "Weerachai";
            String Surname = "Nukitram";

            String urlParameters = "name= " + Name + " &surname=" + Surname;
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("charset", "utf-8");
            conn.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            conn.setUseCaches (false);

            DataOutputStream wr = new DataOutputStream(conn.getOutputStream ());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
/*
            StringBuilder str = new StringBuilder();
            String line;
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            while ((line = reader.readLine()) != null) {
                str.append(line);
            }
*/
//            conn.disconnect();
/*
            System.out.println(str.toString());
*/
        }



        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    private boolean checkInternetConenction()
    {
        // get Connectivity Manager object to check connection
        ConnectivityManager connec  =(ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);

        // Check for network connections
        if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
             connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
             connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
             connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED )
        {
            Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
            return true;
        }
        else if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                  connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  )
        {
            Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
            return false;
        }
        return false;
    }



/*
    <?php
    echo "Sawatdee : ".$_POST["name"]." ".$_POST["surname"];
?>
*/
}