Skip to content

Commit 13b872a

Browse files
committed
HttpUrlConnection + AsyncTask (Kotlin)
HttpUrlConnection + AsyncTask (Kotlin)
1 parent 017c7b0 commit 13b872a

File tree

23 files changed

+180
-133
lines changed

23 files changed

+180
-133
lines changed

android-http/app/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 29
9+
buildToolsVersion "29.0.2"
10+
defaultConfig {
11+
applicationId "com.hmkcode.http"
12+
minSdkVersion 19
13+
targetSdkVersion 29
14+
versionCode 1
15+
versionName "1.0"
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
}
25+
26+
dependencies {
27+
implementation fileTree(dir: 'libs', include: ['*.jar'])
28+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
29+
implementation 'androidx.appcompat:appcompat:1.0.2'
30+
implementation 'androidx.core:core-ktx:1.0.2'
31+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
32+
testImplementation 'junit:junit:4.12'
33+
androidTestImplementation 'androidx.test:runner:1.1.1'
34+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
35+
}

android-http/AndroidManifest.xml renamed to android-http/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.hmkcode.android.http">
3+
package="com.hmkcode.http">
44

55
<uses-permission android:name="android.permission.INTERNET" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -9,7 +9,9 @@
99
android:allowBackup="true"
1010
android:icon="@mipmap/ic_launcher"
1111
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
1213
android:supportsRtl="true"
14+
android:usesCleartextTraffic="true"
1315
android:theme="@style/AppTheme">
1416
<activity android:name=".MainActivity">
1517
<intent-filter>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.hmkcode.http
2+
3+
import android.content.Context
4+
import android.net.ConnectivityManager
5+
import android.net.Network
6+
import android.net.NetworkInfo
7+
import android.os.AsyncTask
8+
import android.os.Build
9+
import androidx.appcompat.app.AppCompatActivity
10+
import android.os.Bundle
11+
import android.widget.TextView
12+
import kotlinx.android.synthetic.main.activity_main.*
13+
import java.io.BufferedReader
14+
import java.io.InputStream
15+
import java.io.InputStreamReader
16+
import java.net.HttpURLConnection
17+
import java.net.URL
18+
19+
class MainActivity : AppCompatActivity() {
20+
21+
lateinit var tvIsConnected: TextView;
22+
lateinit var tvResult: TextView;
23+
24+
25+
override fun onCreate(savedInstanceState: Bundle?) {
26+
super.onCreate(savedInstanceState)
27+
setContentView(R.layout.activity_main)
28+
tvIsConnected = findViewById<TextView>(R.id.tvIsConnected)
29+
tvResult = findViewById<TextView>(R.id.tvResult)
30+
31+
if(checkNetworkConnection())
32+
HTTPAsyncTask().execute("http://hmkcode-api.appspot.com/rest/api/hello/Android")
33+
}
34+
35+
36+
private fun checkNetworkConnection(): Boolean {
37+
val cm:ConnectivityManager =
38+
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
39+
40+
val networkInfo:NetworkInfo? = cm.activeNetworkInfo
41+
val isConnected: Boolean = if(networkInfo != null) networkInfo.isConnected() else false
42+
43+
if(isConnected){
44+
tvIsConnected.setText("Connected "+networkInfo?.typeName)
45+
tvIsConnected.setBackgroundColor(0xFF7CCC26.toInt())
46+
47+
}else{
48+
tvIsConnected.setText("Not Connected!")
49+
tvIsConnected.setBackgroundColor(0xFFFF0000.toInt())
50+
}
51+
return isConnected;
52+
}
53+
54+
inner class HTTPAsyncTask : AsyncTask<String, Void, String>() {
55+
override fun doInBackground(vararg urls: String?): String {
56+
return HttpGet(urls[0])
57+
}
58+
override fun onPostExecute(result: String?) {
59+
tvResult.setText(result)
60+
}
61+
}
62+
63+
private fun HttpGet(myURL: String?): String {
64+
65+
val inputStream:InputStream
66+
val result:String
67+
68+
// create URL
69+
val url:URL = URL(myURL)
70+
71+
// create HttpURLConnection
72+
val conn:HttpURLConnection = url.openConnection() as HttpURLConnection
73+
74+
// make GET request to the given URL
75+
conn.connect()
76+
77+
// receive response as inputStream
78+
inputStream = conn.inputStream
79+
80+
// convert inputstream to string
81+
if(inputStream != null)
82+
result = convertInputStreamToString(inputStream)
83+
else
84+
result = "Did not work!"
85+
86+
return result
87+
}
88+
89+
private fun convertInputStreamToString(inputStream: InputStream): String {
90+
val bufferedReader:BufferedReader? = BufferedReader(InputStreamReader(inputStream))
91+
var line:String? = bufferedReader?.readLine()
92+
var result:String = ""
93+
94+
while (line != null) {
95+
result += line
96+
line = bufferedReader?.readLine()
97+
}
98+
99+
inputStream.close()
100+
return result
101+
}
102+
103+
}

android-http/res/layout/activity_main.xml renamed to android-http/app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
android:paddingRight="@dimen/activity_horizontal_margin"
99
android:paddingTop="@dimen/activity_vertical_margin"
1010
android:orientation="vertical"
11-
tools:context="com.hmkcode.android.http.MainActivity">
12-
13-
11+
tools:context="com.hmkcode.http.MainActivity">
1412

1513
<TextView
1614
android:id="@+id/tvIsConnected"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#008577</color>
4+
<color name="colorPrimaryDark">#00574B</color>
5+
<color name="colorAccent">#D81B60</color>
6+
</resources>

android-http/res/values/dimens.xml renamed to android-http/app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23
<!-- Default screen margins, per the Android Design guidelines. -->
34
<dimen name="activity_horizontal_margin">16dp</dimen>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">android-http</string>
3+
</resources>

android-http/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.kotlin_version = '1.3.50'
5+
repositories {
6+
google()
7+
jcenter()
8+
9+
}
10+
dependencies {
11+
classpath 'com.android.tools.build:gradle:3.5.0'
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13+
// NOTE: Do not place your application dependencies here; they belong
14+
// in the individual module build.gradle files
15+
}
16+
}
17+
18+
allprojects {
19+
repositories {
20+
google()
21+
jcenter()
22+
23+
}
24+
}
25+
26+
task clean(type: Delete) {
27+
delete rootProject.buildDir
28+
}

android-http/java/com/hmkcode/android/http/MainActivity.java

Lines changed: 0 additions & 114 deletions
This file was deleted.

0 commit comments

Comments
 (0)