diff --git a/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/FetchData.java b/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/FetchData.java index 948f725..1dbcb83 100644 --- a/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/FetchData.java +++ b/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/FetchData.java @@ -1,7 +1,5 @@ package com.vishnusivadas.advanced_httpurlconnection; -import android.util.Log; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -10,7 +8,7 @@ import java.net.URL; public class FetchData extends Thread { - private String url; + public String url; String data = "Empty"; public FetchData(String url) { @@ -38,14 +36,23 @@ public void run() { } } - public String getResult() { + public boolean startFetch() { + FetchData.this.start(); + return true; + } + + public boolean onComplete() { while (true) { if (!this.isAlive()) { - return this.getData(); + return true; } } } + public String getResult() { + return this.getData(); + } + public void setData(String data) { this.data = data; } diff --git a/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/PutData.java b/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/PutData.java index 9921eed..e093a60 100644 --- a/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/PutData.java +++ b/Advanced_HttpURLConnection/src/main/java/com/vishnusivadas/advanced_httpurlconnection/PutData.java @@ -60,14 +60,23 @@ public void run() { } } - public String getResult() { + public boolean startFetch() { + PutData.this.start(); + return true; + } + + public boolean onComplete() { while (true) { if (!this.isAlive()) { - return this.getData(); + return true; } } } + public String getResult() { + return this.getData(); + } + public void setData(String result_data) { this.result_data = result_data; } diff --git a/app/src/main/java/com/vishnusivadas/advancedhttpurlconnection/MainActivity.java b/app/src/main/java/com/vishnusivadas/advancedhttpurlconnection/MainActivity.java index f178137..d0d91fa 100644 --- a/app/src/main/java/com/vishnusivadas/advancedhttpurlconnection/MainActivity.java +++ b/app/src/main/java/com/vishnusivadas/advancedhttpurlconnection/MainActivity.java @@ -3,37 +3,85 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.os.Handler; import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ProgressBar; +import android.widget.TextView; import com.vishnusivadas.advanced_httpurlconnection.FetchData; import com.vishnusivadas.advanced_httpurlconnection.PutData; public class MainActivity extends AppCompatActivity { + TextView textView; + ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + textView = findViewById(R.id.dataView); + progressBar = findViewById(R.id.progress); + Button buttonFetch = findViewById(R.id.btnFetchData); + Button buttonPut = findViewById(R.id.btnPutData); + + buttonFetch.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + progressBar.setVisibility(View.VISIBLE); + //Starting Read data from URL + Handler handler = new Handler(); + handler.post(new Runnable() { + @Override + public void run() { + FetchData fetchData = new FetchData("https://projects.vishnusivadas.com/AdvancedHttpURLConnection/readTest.php"); + if (fetchData.startFetch()) { + if (fetchData.onComplete()) { + String result = fetchData.getResult(); + progressBar.setVisibility(View.GONE); + textView.setText(result); + Log.i("FetchData", result); + } + } + } + }); + //End Read data from URL + } + }); + + buttonPut.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + progressBar.setVisibility(View.VISIBLE); + Handler handler = new Handler(); + handler.post(new Runnable() { + @Override + public void run() { + //Starting Write and Read data with URL + //Creating array for parameters + String[] param = new String[2]; + param[0] = "param-1"; + param[1] = "param-2"; + //Creating array for data + String[] data = new String[2]; + data[0] = "data-1"; + data[1] = "data-2"; + PutData putData = new PutData("https://projects.vishnusivadas.com/AdvancedHttpURLConnection/putDataTest.php", "GET", param, data); + if (putData.startFetch()) { + if (putData.onComplete()) { + String result = putData.getResult(); + progressBar.setVisibility(View.GONE); + textView.setText(result); + Log.i("PutData", result); + } + } + //End Write and Read data with URL + } + }); + } + }); - //Starting Read data from URL - FetchData fetchData = new FetchData("https://projects.vishnusivadas.com/AdvancedHttpURLConnection/readTest.php"); - fetchData.start(); - Log.i("FetchData", fetchData.getResult()); - //End Read data from URL - - //Starting Write and Read data with URL - //Creating array for parameters - String[] param = new String[2]; - param[0] = "param-1"; - param[1] = "param-2"; - //Creating array for data - String[] data = new String[2]; - data[0] = "data-1"; - data[1] = "data-2"; - PutData putData = new PutData("https://projects.vishnusivadas.com/AdvancedHttpURLConnection/putDataTest.php", "GET", param, data); - putData.start(); - Log.i("PutData", putData.getResult()); - //End Write and Read data with URL } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4fc2444..0ef9fa6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,42 @@ - + + android:text="@string/data" /> + +