Skip to content

Commit 51ef170

Browse files
authored
feat: add http request activity (#20)
1 parent f17ecd9 commit 51ef170

File tree

5 files changed

+155
-1
lines changed

5 files changed

+155
-1
lines changed

android_app_bootstrap/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
android:name=".activity.DataHubActivity"
5656
android:configChanges="orientation|keyboardHidden"
5757
android:screenOrientation="portrait"></activity>
58+
<activity
59+
android:name=".activity.HttpRequestActivity"
60+
android:configChanges="orientation|keyboardHidden"
61+
android:screenOrientation="portrait"></activity>
5862
<activity
5963
android:name=".activity.GestureActivity"
6064
android:configChanges="orientation|keyboardHidden"
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.github.android_app_bootstrap.activity;
2+
3+
/**
4+
* Created by xdf on 10/8/15.
5+
*/
6+
7+
import android.app.Activity;
8+
import android.os.AsyncTask;
9+
import android.os.Bundle;
10+
import android.util.Log;
11+
import android.view.Gravity;
12+
import android.view.View;
13+
import android.widget.Button;
14+
import android.widget.TextView;
15+
16+
import com.alibaba.fastjson.JSON;
17+
import com.alibaba.fastjson.JSONObject;
18+
import com.github.android_app_bootstrap.R;
19+
20+
import java.io.IOException;
21+
22+
import okhttp3.OkHttpClient;
23+
import okhttp3.Request;
24+
import okhttp3.Response;
25+
26+
public class HttpRequestActivity extends Activity implements View.OnClickListener {
27+
28+
private static final String API = "https://httpbin.org/get";
29+
30+
private static final String TAG = "request";
31+
32+
private TextView show_result;
33+
34+
class Task extends AsyncTask<Integer, Integer, String> {
35+
public Task() {
36+
}
37+
38+
@Override
39+
protected String doInBackground(Integer... params) {
40+
String lastVersionStr = null;
41+
Log.i(TAG, API);
42+
43+
OkHttpClient client = new OkHttpClient();
44+
45+
try {
46+
Request request = new Request.Builder()
47+
.url(API)
48+
.get()
49+
.build();
50+
51+
Response response = client.newCall(request).execute();
52+
JSONObject res = JSON.parseObject(response.body().string());
53+
Log.i(TAG, res.toJSONString());
54+
lastVersionStr = res.toJSONString();
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
59+
return lastVersionStr;
60+
}
61+
62+
@Override
63+
protected void onPostExecute(String lastVersionStr) {
64+
show_result.setText(lastVersionStr);
65+
}
66+
}
67+
68+
public void onCreate(Bundle savedInstanceState) {
69+
super.onCreate(savedInstanceState);
70+
setContentView(R.layout.request_activity);
71+
show_result = (TextView) findViewById(R.id.result);
72+
initView();
73+
}
74+
75+
public void initView() {
76+
Button button = (Button) findViewById(R.id.request);
77+
button.setOnClickListener(this);
78+
}
79+
80+
void doRequest() {
81+
Task task = new Task();
82+
task.execute(200);
83+
}
84+
85+
@Override
86+
public void onClick(View view) {
87+
switch (view.getId()) {
88+
case R.id.request:
89+
doRequest();
90+
break;
91+
}
92+
}
93+
}

android_app_bootstrap/src/main/java/com/github/android_app_bootstrap/activity/TableActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
5656
Intent dataHubIntent = new Intent(TableActivity.this, DataHubActivity.class);
5757
startActivity(dataHubIntent);
5858
break;
59+
case 5:
60+
Intent httpRequestIntent = new Intent(TableActivity.this, HttpRequestActivity.class);
61+
startActivity(httpRequestIntent);
62+
break;
5963
}
6064
}
6165
});
@@ -69,6 +73,7 @@ private List<String> getData() {
6973
data.add("Alert");
7074
data.add("Location");
7175
data.add("DataHub");
76+
data.add("HTTP request");
7277
data.add("Test test test");
7378
data.add("Test test test");
7479
data.add("Test test test");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<LinearLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:layout_marginTop="100dp"
11+
android:orientation="horizontal"
12+
android:paddingLeft="30dp"
13+
android:paddingRight="30dp">
14+
15+
<Button
16+
android:id="@+id/request"
17+
android:layout_width="match_parent"
18+
android:layout_height="38dp"
19+
android:background="@drawable/round_corner_bg"
20+
android:gravity="center_vertical|center"
21+
android:text="@string/request"
22+
android:textColor="@color/white"
23+
android:textSize="18dp" />
24+
</LinearLayout>
25+
26+
<LinearLayout
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:layout_marginTop="10dp"
30+
android:orientation="horizontal"
31+
android:paddingLeft="30dp"
32+
android:paddingRight="30dp"/>
33+
34+
<LinearLayout
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_marginTop="50dp"
38+
android:orientation="horizontal">
39+
40+
<TextView
41+
android:id="@+id/result"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:gravity="left"
45+
android:textSize="20dp"
46+
android:minHeight="200dp"
47+
android:background="@color/aliceblue"
48+
android:layout_marginStart="20dp"
49+
android:layout_marginRight="20dp"
50+
android:text="@string/blank" />
51+
</LinearLayout>
52+
</LinearLayout>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"devDependencies": {
2121
"git-contributor": "1",
22-
"macaca-datahub": "3",
22+
"macaca-datahub": "4",
2323
"reliable-cli": "1"
2424
},
2525
"license": "MIT"

0 commit comments

Comments
 (0)