diff --git a/android-http/AndroidManifest.xml b/android-http/AndroidManifest.xml new file mode 100644 index 0000000..bcec53c --- /dev/null +++ b/android-http/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android-http/java/com/hmkcode/android/http/MainActivity.java b/android-http/java/com/hmkcode/android/http/MainActivity.java new file mode 100644 index 0000000..b459e2c --- /dev/null +++ b/android-http/java/com/hmkcode/android/http/MainActivity.java @@ -0,0 +1,114 @@ + package com.hmkcode.android.http; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.AsyncTask; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.widget.TextView; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + + public class MainActivity extends AppCompatActivity { + + TextView tvIsConnected; + TextView tvResult; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + tvIsConnected = (TextView) findViewById(R.id.tvIsConnected); + tvResult = (TextView) findViewById(R.id.tvResult); + if(checkNetworkConnection()) + // perform HTTP GET request + new HTTPAsyncTask().execute("http://hmkcode.com/examples/index.php"); + } + + + // check network connection + public boolean checkNetworkConnection() { + ConnectivityManager connMgr = (ConnectivityManager) + getSystemService(Context.CONNECTIVITY_SERVICE); + + NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); + boolean isConnected = false; + if (networkInfo != null && (isConnected = networkInfo.isConnected())) { + // show "Connected" & type of network "WIFI or MOBILE" + tvIsConnected.setText("Connected "+networkInfo.getTypeName()); + // change background color to red + tvIsConnected.setBackgroundColor(0xFF7CCC26); + + + } else { + // show "Not Connected" + tvIsConnected.setText("Not Connected"); + // change background color to green + tvIsConnected.setBackgroundColor(0xFFFF0000); + } + + return isConnected; + } + + private class HTTPAsyncTask extends AsyncTask { + @Override + protected String doInBackground(String... urls) { + + // params comes from the execute() call: params[0] is the url. + try { + return HttpGet(urls[0]); + } catch (IOException e) { + return "Unable to retrieve web page. URL may be invalid."; + } + } + // onPostExecute displays the results of the AsyncTask. + @Override + protected void onPostExecute(String result) { + tvResult.setText(result); + } + } + + private String HttpGet(String myUrl) throws IOException { + InputStream inputStream = null; + String result = ""; + + URL url = new URL(myUrl); + + // create HttpURLConnection + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + + // make GET request to the given URL + conn.connect(); + + // receive response as inputStream + inputStream = conn.getInputStream(); + + // convert inputstream to string + if(inputStream != null) + result = convertInputStreamToString(inputStream); + else + result = "Did not work!"; + + return result; + } + + private static String convertInputStreamToString(InputStream inputStream) throws IOException{ + BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); + String line = ""; + String result = ""; + while((line = bufferedReader.readLine()) != null) + result += line; + + inputStream.close(); + return result; + + } + + + } diff --git a/android-http/res/layout/activity_main.xml b/android-http/res/layout/activity_main.xml new file mode 100644 index 0000000..cb63217 --- /dev/null +++ b/android-http/res/layout/activity_main.xml @@ -0,0 +1,38 @@ + + + + + + + + + diff --git a/android-http/res/mipmap-hdpi/Thumbs.db b/android-http/res/mipmap-hdpi/Thumbs.db new file mode 100644 index 0000000..c7be653 Binary files /dev/null and b/android-http/res/mipmap-hdpi/Thumbs.db differ diff --git a/android-http/res/mipmap-hdpi/ic_launcher.png b/android-http/res/mipmap-hdpi/ic_launcher.png new file mode 100644 index 0000000..cde69bc Binary files /dev/null and b/android-http/res/mipmap-hdpi/ic_launcher.png differ diff --git a/android-http/res/mipmap-mdpi/Thumbs.db b/android-http/res/mipmap-mdpi/Thumbs.db new file mode 100644 index 0000000..6b7a25c Binary files /dev/null and b/android-http/res/mipmap-mdpi/Thumbs.db differ diff --git a/android-http/res/mipmap-mdpi/ic_launcher.png b/android-http/res/mipmap-mdpi/ic_launcher.png new file mode 100644 index 0000000..c133a0c Binary files /dev/null and b/android-http/res/mipmap-mdpi/ic_launcher.png differ diff --git a/android-http/res/mipmap-xhdpi/Thumbs.db b/android-http/res/mipmap-xhdpi/Thumbs.db new file mode 100644 index 0000000..955fa41 Binary files /dev/null and b/android-http/res/mipmap-xhdpi/Thumbs.db differ diff --git a/android-http/res/mipmap-xhdpi/ic_launcher.png b/android-http/res/mipmap-xhdpi/ic_launcher.png new file mode 100644 index 0000000..bfa42f0 Binary files /dev/null and b/android-http/res/mipmap-xhdpi/ic_launcher.png differ diff --git a/android-http/res/mipmap-xxhdpi/Thumbs.db b/android-http/res/mipmap-xxhdpi/Thumbs.db new file mode 100644 index 0000000..3b89207 Binary files /dev/null and b/android-http/res/mipmap-xxhdpi/Thumbs.db differ diff --git a/android-http/res/mipmap-xxhdpi/ic_launcher.png b/android-http/res/mipmap-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..324e72c Binary files /dev/null and b/android-http/res/mipmap-xxhdpi/ic_launcher.png differ diff --git a/android-http/res/mipmap-xxxhdpi/Thumbs.db b/android-http/res/mipmap-xxxhdpi/Thumbs.db new file mode 100644 index 0000000..65eaac7 Binary files /dev/null and b/android-http/res/mipmap-xxxhdpi/Thumbs.db differ diff --git a/android-http/res/mipmap-xxxhdpi/ic_launcher.png b/android-http/res/mipmap-xxxhdpi/ic_launcher.png new file mode 100644 index 0000000..aee44e1 Binary files /dev/null and b/android-http/res/mipmap-xxxhdpi/ic_launcher.png differ diff --git a/android-http/res/values-w820dp/dimens.xml b/android-http/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/android-http/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/android-http/res/values/colors.xml b/android-http/res/values/colors.xml new file mode 100644 index 0000000..3ab3e9c --- /dev/null +++ b/android-http/res/values/colors.xml @@ -0,0 +1,6 @@ + + + #3F51B5 + #303F9F + #FF4081 + diff --git a/android-http/res/values/dimens.xml b/android-http/res/values/dimens.xml new file mode 100644 index 0000000..47c8224 --- /dev/null +++ b/android-http/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 16dp + 16dp + diff --git a/android-http/res/values/strings.xml b/android-http/res/values/strings.xml new file mode 100644 index 0000000..a621270 --- /dev/null +++ b/android-http/res/values/strings.xml @@ -0,0 +1,3 @@ + + Android HTTP + diff --git a/android-http/res/values/styles.xml b/android-http/res/values/styles.xml new file mode 100644 index 0000000..5885930 --- /dev/null +++ b/android-http/res/values/styles.xml @@ -0,0 +1,11 @@ + + + + + +