Skip to content

Commit 657c373

Browse files
committed
post-json
1 parent 6849239 commit 657c373

File tree

10 files changed

+475
-1
lines changed

10 files changed

+475
-1
lines changed

android-http/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88

99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.5.0'
11+
classpath 'com.android.tools.build:gradle:3.5.1'
1212
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
// NOTE: Do not place your application dependencies here; they belong
1414
// in the individual module build.gradle files

android-http/post-json/build.gradle

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
android {
5+
compileSdkVersion 29
6+
buildToolsVersion "29.0.2"
7+
8+
9+
defaultConfig {
10+
applicationId "com.hmkcodes"
11+
minSdkVersion 19
12+
targetSdkVersion 29
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
26+
}
27+
28+
dependencies {
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31+
implementation 'androidx.appcompat:appcompat:1.1.0'
32+
implementation 'com.google.android.material:material:1.1.0-beta01'
33+
implementation 'androidx.core:core-ktx:1.1.0'
34+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
35+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2"
36+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
37+
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha05"
38+
39+
testImplementation 'junit:junit:4.12'
40+
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
41+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hmkcode">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:usesCleartextTraffic="true"
15+
android:theme="@style/AppTheme">
16+
<activity android:name=".MainActivity">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.hmkcode
2+
3+
import android.content.Context
4+
import android.net.ConnectivityManager
5+
import androidx.appcompat.app.AppCompatActivity
6+
import android.os.Bundle
7+
import android.util.Log
8+
import android.view.View
9+
import android.widget.EditText
10+
import android.widget.TextView
11+
import android.widget.Toast
12+
import androidx.lifecycle.lifecycleScope
13+
import kotlinx.coroutines.Dispatchers
14+
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.withContext
16+
import org.json.JSONException
17+
import org.json.JSONObject
18+
import java.io.BufferedWriter
19+
import java.io.IOException
20+
import java.io.OutputStreamWriter
21+
import java.net.HttpURLConnection
22+
import java.net.URL
23+
import javax.net.ssl.HttpsURLConnection
24+
25+
class MainActivity : AppCompatActivity() {
26+
27+
lateinit var tvIsConnected: TextView
28+
lateinit var etTitle: EditText
29+
lateinit var etUrl: EditText
30+
lateinit var etTags: EditText
31+
lateinit var tvResult: TextView
32+
33+
override fun onCreate(savedInstanceState: Bundle?) {
34+
super.onCreate(savedInstanceState)
35+
setContentView(R.layout.activity_main)
36+
37+
tvIsConnected = findViewById<TextView>(R.id.tvIsConnected)
38+
etTitle = findViewById<EditText>(R.id.etTitle)
39+
etUrl = findViewById<EditText>(R.id.etUrl)
40+
etTags = findViewById<EditText>(R.id.etTags)
41+
tvResult = findViewById<TextView>(R.id.tvResult)
42+
checkNetworkConnection()
43+
}
44+
45+
public fun send(view:View) {
46+
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show()
47+
// clear text result
48+
tvResult.setText("")
49+
50+
if (checkNetworkConnection())
51+
lifecycleScope.launch {
52+
val result = httpPost("https://hmkcode-api.appspot.com/rest/link/add")
53+
tvResult.setText(result)
54+
}
55+
else
56+
Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show()
57+
58+
}
59+
60+
@Throws(IOException::class, JSONException::class)
61+
private suspend fun httpPost(myUrl: String): String {
62+
63+
val result = withContext(Dispatchers.IO) {
64+
val url = URL(myUrl)
65+
// 1. create HttpURLConnection
66+
val conn = url.openConnection() as HttpsURLConnection
67+
conn.requestMethod = "POST"
68+
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8")
69+
70+
// 2. build JSON object
71+
val jsonObject = buidJsonObject()
72+
73+
// 3. add JSON content to POST request body
74+
setPostRequestContent(conn, jsonObject)
75+
76+
// 4. make POST request to the given URL
77+
conn.connect()
78+
79+
// 5. return response message
80+
conn.responseMessage + ""
81+
}
82+
return result
83+
}
84+
85+
private fun checkNetworkConnection(): Boolean {
86+
val connMgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
87+
88+
val networkInfo = connMgr.activeNetworkInfo
89+
val isConnected: Boolean = if(networkInfo != null) networkInfo.isConnected() else false
90+
if (networkInfo != null && isConnected) {
91+
// show "Connected" & type of network "WIFI or MOBILE"
92+
tvIsConnected.text = "Connected " + networkInfo.typeName
93+
// change background color to red
94+
tvIsConnected.setBackgroundColor(-0x8333da)
95+
96+
97+
} else {
98+
// show "Not Connected"
99+
tvIsConnected.text = "Not Connected"
100+
// change background color to green
101+
tvIsConnected.setBackgroundColor(-0x10000)
102+
}
103+
104+
return isConnected
105+
}
106+
107+
@Throws(JSONException::class)
108+
private fun buidJsonObject(): JSONObject {
109+
110+
val jsonObject = JSONObject()
111+
jsonObject.accumulate("title", etTitle.getText().toString())
112+
jsonObject.accumulate("url", etUrl.getText().toString())
113+
jsonObject.accumulate("tags", etTags.getText().toString())
114+
115+
return jsonObject
116+
}
117+
118+
@Throws(IOException::class)
119+
private fun setPostRequestContent(conn: HttpURLConnection, jsonObject: JSONObject) {
120+
121+
val os = conn.outputStream
122+
val writer = BufferedWriter(OutputStreamWriter(os, "UTF-8"))
123+
writer.write(jsonObject.toString())
124+
Log.i(MainActivity::class.java.toString(), jsonObject.toString())
125+
writer.flush()
126+
writer.close()
127+
os.close()
128+
}
129+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillColor="#008577"
9+
android:pathData="M0,0h108v108h-108z" />
10+
<path
11+
android:fillColor="#00000000"
12+
android:pathData="M9,0L9,108"
13+
android:strokeWidth="0.8"
14+
android:strokeColor="#33FFFFFF" />
15+
<path
16+
android:fillColor="#00000000"
17+
android:pathData="M19,0L19,108"
18+
android:strokeWidth="0.8"
19+
android:strokeColor="#33FFFFFF" />
20+
<path
21+
android:fillColor="#00000000"
22+
android:pathData="M29,0L29,108"
23+
android:strokeWidth="0.8"
24+
android:strokeColor="#33FFFFFF" />
25+
<path
26+
android:fillColor="#00000000"
27+
android:pathData="M39,0L39,108"
28+
android:strokeWidth="0.8"
29+
android:strokeColor="#33FFFFFF" />
30+
<path
31+
android:fillColor="#00000000"
32+
android:pathData="M49,0L49,108"
33+
android:strokeWidth="0.8"
34+
android:strokeColor="#33FFFFFF" />
35+
<path
36+
android:fillColor="#00000000"
37+
android:pathData="M59,0L59,108"
38+
android:strokeWidth="0.8"
39+
android:strokeColor="#33FFFFFF" />
40+
<path
41+
android:fillColor="#00000000"
42+
android:pathData="M69,0L69,108"
43+
android:strokeWidth="0.8"
44+
android:strokeColor="#33FFFFFF" />
45+
<path
46+
android:fillColor="#00000000"
47+
android:pathData="M79,0L79,108"
48+
android:strokeWidth="0.8"
49+
android:strokeColor="#33FFFFFF" />
50+
<path
51+
android:fillColor="#00000000"
52+
android:pathData="M89,0L89,108"
53+
android:strokeWidth="0.8"
54+
android:strokeColor="#33FFFFFF" />
55+
<path
56+
android:fillColor="#00000000"
57+
android:pathData="M99,0L99,108"
58+
android:strokeWidth="0.8"
59+
android:strokeColor="#33FFFFFF" />
60+
<path
61+
android:fillColor="#00000000"
62+
android:pathData="M0,9L108,9"
63+
android:strokeWidth="0.8"
64+
android:strokeColor="#33FFFFFF" />
65+
<path
66+
android:fillColor="#00000000"
67+
android:pathData="M0,19L108,19"
68+
android:strokeWidth="0.8"
69+
android:strokeColor="#33FFFFFF" />
70+
<path
71+
android:fillColor="#00000000"
72+
android:pathData="M0,29L108,29"
73+
android:strokeWidth="0.8"
74+
android:strokeColor="#33FFFFFF" />
75+
<path
76+
android:fillColor="#00000000"
77+
android:pathData="M0,39L108,39"
78+
android:strokeWidth="0.8"
79+
android:strokeColor="#33FFFFFF" />
80+
<path
81+
android:fillColor="#00000000"
82+
android:pathData="M0,49L108,49"
83+
android:strokeWidth="0.8"
84+
android:strokeColor="#33FFFFFF" />
85+
<path
86+
android:fillColor="#00000000"
87+
android:pathData="M0,59L108,59"
88+
android:strokeWidth="0.8"
89+
android:strokeColor="#33FFFFFF" />
90+
<path
91+
android:fillColor="#00000000"
92+
android:pathData="M0,69L108,69"
93+
android:strokeWidth="0.8"
94+
android:strokeColor="#33FFFFFF" />
95+
<path
96+
android:fillColor="#00000000"
97+
android:pathData="M0,79L108,79"
98+
android:strokeWidth="0.8"
99+
android:strokeColor="#33FFFFFF" />
100+
<path
101+
android:fillColor="#00000000"
102+
android:pathData="M0,89L108,89"
103+
android:strokeWidth="0.8"
104+
android:strokeColor="#33FFFFFF" />
105+
<path
106+
android:fillColor="#00000000"
107+
android:pathData="M0,99L108,99"
108+
android:strokeWidth="0.8"
109+
android:strokeColor="#33FFFFFF" />
110+
<path
111+
android:fillColor="#00000000"
112+
android:pathData="M19,29L89,29"
113+
android:strokeWidth="0.8"
114+
android:strokeColor="#33FFFFFF" />
115+
<path
116+
android:fillColor="#00000000"
117+
android:pathData="M19,39L89,39"
118+
android:strokeWidth="0.8"
119+
android:strokeColor="#33FFFFFF" />
120+
<path
121+
android:fillColor="#00000000"
122+
android:pathData="M19,49L89,49"
123+
android:strokeWidth="0.8"
124+
android:strokeColor="#33FFFFFF" />
125+
<path
126+
android:fillColor="#00000000"
127+
android:pathData="M19,59L89,59"
128+
android:strokeWidth="0.8"
129+
android:strokeColor="#33FFFFFF" />
130+
<path
131+
android:fillColor="#00000000"
132+
android:pathData="M19,69L89,69"
133+
android:strokeWidth="0.8"
134+
android:strokeColor="#33FFFFFF" />
135+
<path
136+
android:fillColor="#00000000"
137+
android:pathData="M19,79L89,79"
138+
android:strokeWidth="0.8"
139+
android:strokeColor="#33FFFFFF" />
140+
<path
141+
android:fillColor="#00000000"
142+
android:pathData="M29,19L29,89"
143+
android:strokeWidth="0.8"
144+
android:strokeColor="#33FFFFFF" />
145+
<path
146+
android:fillColor="#00000000"
147+
android:pathData="M39,19L39,89"
148+
android:strokeWidth="0.8"
149+
android:strokeColor="#33FFFFFF" />
150+
<path
151+
android:fillColor="#00000000"
152+
android:pathData="M49,19L49,89"
153+
android:strokeWidth="0.8"
154+
android:strokeColor="#33FFFFFF" />
155+
<path
156+
android:fillColor="#00000000"
157+
android:pathData="M59,19L59,89"
158+
android:strokeWidth="0.8"
159+
android:strokeColor="#33FFFFFF" />
160+
<path
161+
android:fillColor="#00000000"
162+
android:pathData="M69,19L69,89"
163+
android:strokeWidth="0.8"
164+
android:strokeColor="#33FFFFFF" />
165+
<path
166+
android:fillColor="#00000000"
167+
android:pathData="M79,19L79,89"
168+
android:strokeWidth="0.8"
169+
android:strokeColor="#33FFFFFF" />
170+
</vector>

0 commit comments

Comments
 (0)