Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSivadasVS committed Jun 24, 2020
1 parent f59982e commit 57cc208
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}
40 changes: 32 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/progress"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/dataView"
android:textSize="30sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:text="@string/data" />

<Button
android:id="@+id/btnFetchData"
android:text="@string/fetch_data"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/Widget.AppCompat.Button.Colored"/>
<Button
android:id="@+id/btnPutData"
android:text="@string/put_data"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/Widget.AppCompat.Button.Colored"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorPrimary">#01A899</color>
<color name="colorPrimaryDark">#009688</color>
<color name="colorAccent">#03DAC5</color>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<resources>
<string name="app_name">AdvancedHttpURLConnection</string>
<string name="data">Fetching Data</string>
<string name="fetch_data">Fetch Data</string>
<string name="put_data">Put Data</string>
</resources>

0 comments on commit 57cc208

Please sign in to comment.