Skip to content

Commit 17b7e4d

Browse files
committed
android post json updated
updated with new tools and API
1 parent 1339ed3 commit 17b7e4d

33 files changed

+509
-6823
lines changed

android-post-json/app/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 27
5+
defaultConfig {
6+
applicationId "com.hmkcode"
7+
minSdkVersion 16
8+
targetSdkVersion 27
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:27.1.1'
24+
implementation 'com.android.support:design:27.1.1'
25+
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
26+
testImplementation 'junit:junit:4.12'
27+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
28+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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:theme="@style/AppTheme">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.hmkcode;
2+
3+
import android.content.Context;
4+
import android.net.ConnectivityManager;
5+
import android.net.NetworkInfo;
6+
import android.os.AsyncTask;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.os.Bundle;
9+
import android.util.Log;
10+
import android.view.View;
11+
import android.widget.EditText;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
14+
15+
import org.json.JSONException;
16+
import org.json.JSONObject;
17+
18+
import java.io.BufferedReader;
19+
import java.io.BufferedWriter;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.InputStreamReader;
23+
import java.io.OutputStream;
24+
import java.io.OutputStreamWriter;
25+
import java.net.HttpURLConnection;
26+
import java.net.URL;
27+
28+
public class MainActivity extends AppCompatActivity {
29+
30+
TextView tvIsConnected;
31+
EditText etName;
32+
EditText etCountry;
33+
EditText etTwitter;
34+
TextView tvResult;
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.activity_main);
39+
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
40+
etName = findViewById(R.id.etName);
41+
etCountry = findViewById(R.id.etCountry);
42+
etTwitter = findViewById(R.id.etTwitter);
43+
tvResult = (TextView) findViewById(R.id.tvResult);
44+
checkNetworkConnection();
45+
46+
47+
48+
}
49+
50+
// check network connection
51+
public boolean checkNetworkConnection() {
52+
ConnectivityManager connMgr = (ConnectivityManager)
53+
getSystemService(Context.CONNECTIVITY_SERVICE);
54+
55+
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
56+
boolean isConnected = false;
57+
if (networkInfo != null && (isConnected = networkInfo.isConnected())) {
58+
// show "Connected" & type of network "WIFI or MOBILE"
59+
tvIsConnected.setText("Connected "+networkInfo.getTypeName());
60+
// change background color to red
61+
tvIsConnected.setBackgroundColor(0xFF7CCC26);
62+
63+
64+
} else {
65+
// show "Not Connected"
66+
tvIsConnected.setText("Not Connected");
67+
// change background color to green
68+
tvIsConnected.setBackgroundColor(0xFFFF0000);
69+
}
70+
71+
return isConnected;
72+
}
73+
74+
75+
private String httpPost(String myUrl) throws IOException, JSONException {
76+
String result = "";
77+
78+
URL url = new URL(myUrl);
79+
80+
// 1. create HttpURLConnection
81+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
82+
conn.setRequestMethod("POST");
83+
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
84+
85+
// 2. build JSON object
86+
JSONObject jsonObject = buidJsonObject();
87+
88+
// 3. add JSON content to POST request body
89+
setPostRequestContent(conn, jsonObject);
90+
91+
// 4. make POST request to the given URL
92+
conn.connect();
93+
94+
// 5. return response message
95+
return conn.getResponseMessage()+"";
96+
97+
}
98+
99+
100+
private class HTTPAsyncTask extends AsyncTask<String, Void, String> {
101+
@Override
102+
protected String doInBackground(String... urls) {
103+
// params comes from the execute() call: params[0] is the url.
104+
try {
105+
try {
106+
return httpPost(urls[0]);
107+
} catch (JSONException e) {
108+
e.printStackTrace();
109+
return "Error!";
110+
}
111+
} catch (IOException e) {
112+
return "Unable to retrieve web page. URL may be invalid.";
113+
}
114+
}
115+
// onPostExecute displays the results of the AsyncTask.
116+
@Override
117+
protected void onPostExecute(String result) {
118+
tvResult.setText(result);
119+
}
120+
}
121+
122+
123+
public void send(View view) {
124+
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
125+
// perform HTTP POST request
126+
if(checkNetworkConnection())
127+
new HTTPAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet");
128+
else
129+
Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show();
130+
131+
}
132+
133+
private JSONObject buidJsonObject() throws JSONException {
134+
135+
JSONObject jsonObject = new JSONObject();
136+
jsonObject.accumulate("name", etName.getText().toString());
137+
jsonObject.accumulate("country", etCountry.getText().toString());
138+
jsonObject.accumulate("twitter", etTwitter.getText().toString());
139+
140+
return jsonObject;
141+
}
142+
143+
private void setPostRequestContent(HttpURLConnection conn, JSONObject jsonObject) throws IOException {
144+
145+
OutputStream os = conn.getOutputStream();
146+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
147+
writer.write(jsonObject.toString());
148+
Log.i(MainActivity.class.toString(), jsonObject.toString());
149+
writer.flush();
150+
writer.close();
151+
os.close();
152+
}
153+
154+
}
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:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillColor="#26A69A"
9+
android:pathData="M0,0h108v108h-108z" />
10+
<path
11+
android:fillColor="#00000000"
12+
android:pathData="M9,0L9,108"
13+
android:strokeColor="#33FFFFFF"
14+
android:strokeWidth="0.8" />
15+
<path
16+
android:fillColor="#00000000"
17+
android:pathData="M19,0L19,108"
18+
android:strokeColor="#33FFFFFF"
19+
android:strokeWidth="0.8" />
20+
<path
21+
android:fillColor="#00000000"
22+
android:pathData="M29,0L29,108"
23+
android:strokeColor="#33FFFFFF"
24+
android:strokeWidth="0.8" />
25+
<path
26+
android:fillColor="#00000000"
27+
android:pathData="M39,0L39,108"
28+
android:strokeColor="#33FFFFFF"
29+
android:strokeWidth="0.8" />
30+
<path
31+
android:fillColor="#00000000"
32+
android:pathData="M49,0L49,108"
33+
android:strokeColor="#33FFFFFF"
34+
android:strokeWidth="0.8" />
35+
<path
36+
android:fillColor="#00000000"
37+
android:pathData="M59,0L59,108"
38+
android:strokeColor="#33FFFFFF"
39+
android:strokeWidth="0.8" />
40+
<path
41+
android:fillColor="#00000000"
42+
android:pathData="M69,0L69,108"
43+
android:strokeColor="#33FFFFFF"
44+
android:strokeWidth="0.8" />
45+
<path
46+
android:fillColor="#00000000"
47+
android:pathData="M79,0L79,108"
48+
android:strokeColor="#33FFFFFF"
49+
android:strokeWidth="0.8" />
50+
<path
51+
android:fillColor="#00000000"
52+
android:pathData="M89,0L89,108"
53+
android:strokeColor="#33FFFFFF"
54+
android:strokeWidth="0.8" />
55+
<path
56+
android:fillColor="#00000000"
57+
android:pathData="M99,0L99,108"
58+
android:strokeColor="#33FFFFFF"
59+
android:strokeWidth="0.8" />
60+
<path
61+
android:fillColor="#00000000"
62+
android:pathData="M0,9L108,9"
63+
android:strokeColor="#33FFFFFF"
64+
android:strokeWidth="0.8" />
65+
<path
66+
android:fillColor="#00000000"
67+
android:pathData="M0,19L108,19"
68+
android:strokeColor="#33FFFFFF"
69+
android:strokeWidth="0.8" />
70+
<path
71+
android:fillColor="#00000000"
72+
android:pathData="M0,29L108,29"
73+
android:strokeColor="#33FFFFFF"
74+
android:strokeWidth="0.8" />
75+
<path
76+
android:fillColor="#00000000"
77+
android:pathData="M0,39L108,39"
78+
android:strokeColor="#33FFFFFF"
79+
android:strokeWidth="0.8" />
80+
<path
81+
android:fillColor="#00000000"
82+
android:pathData="M0,49L108,49"
83+
android:strokeColor="#33FFFFFF"
84+
android:strokeWidth="0.8" />
85+
<path
86+
android:fillColor="#00000000"
87+
android:pathData="M0,59L108,59"
88+
android:strokeColor="#33FFFFFF"
89+
android:strokeWidth="0.8" />
90+
<path
91+
android:fillColor="#00000000"
92+
android:pathData="M0,69L108,69"
93+
android:strokeColor="#33FFFFFF"
94+
android:strokeWidth="0.8" />
95+
<path
96+
android:fillColor="#00000000"
97+
android:pathData="M0,79L108,79"
98+
android:strokeColor="#33FFFFFF"
99+
android:strokeWidth="0.8" />
100+
<path
101+
android:fillColor="#00000000"
102+
android:pathData="M0,89L108,89"
103+
android:strokeColor="#33FFFFFF"
104+
android:strokeWidth="0.8" />
105+
<path
106+
android:fillColor="#00000000"
107+
android:pathData="M0,99L108,99"
108+
android:strokeColor="#33FFFFFF"
109+
android:strokeWidth="0.8" />
110+
<path
111+
android:fillColor="#00000000"
112+
android:pathData="M19,29L89,29"
113+
android:strokeColor="#33FFFFFF"
114+
android:strokeWidth="0.8" />
115+
<path
116+
android:fillColor="#00000000"
117+
android:pathData="M19,39L89,39"
118+
android:strokeColor="#33FFFFFF"
119+
android:strokeWidth="0.8" />
120+
<path
121+
android:fillColor="#00000000"
122+
android:pathData="M19,49L89,49"
123+
android:strokeColor="#33FFFFFF"
124+
android:strokeWidth="0.8" />
125+
<path
126+
android:fillColor="#00000000"
127+
android:pathData="M19,59L89,59"
128+
android:strokeColor="#33FFFFFF"
129+
android:strokeWidth="0.8" />
130+
<path
131+
android:fillColor="#00000000"
132+
android:pathData="M19,69L89,69"
133+
android:strokeColor="#33FFFFFF"
134+
android:strokeWidth="0.8" />
135+
<path
136+
android:fillColor="#00000000"
137+
android:pathData="M19,79L89,79"
138+
android:strokeColor="#33FFFFFF"
139+
android:strokeWidth="0.8" />
140+
<path
141+
android:fillColor="#00000000"
142+
android:pathData="M29,19L29,89"
143+
android:strokeColor="#33FFFFFF"
144+
android:strokeWidth="0.8" />
145+
<path
146+
android:fillColor="#00000000"
147+
android:pathData="M39,19L39,89"
148+
android:strokeColor="#33FFFFFF"
149+
android:strokeWidth="0.8" />
150+
<path
151+
android:fillColor="#00000000"
152+
android:pathData="M49,19L49,89"
153+
android:strokeColor="#33FFFFFF"
154+
android:strokeWidth="0.8" />
155+
<path
156+
android:fillColor="#00000000"
157+
android:pathData="M59,19L59,89"
158+
android:strokeColor="#33FFFFFF"
159+
android:strokeWidth="0.8" />
160+
<path
161+
android:fillColor="#00000000"
162+
android:pathData="M69,19L69,89"
163+
android:strokeColor="#33FFFFFF"
164+
android:strokeWidth="0.8" />
165+
<path
166+
android:fillColor="#00000000"
167+
android:pathData="M79,19L79,89"
168+
android:strokeColor="#33FFFFFF"
169+
android:strokeWidth="0.8" />
170+
</vector>

0 commit comments

Comments
 (0)