Skip to content

Commit 9c58574

Browse files
committed
Android:APIJSONTest 新增切换屏幕方向及在 Auto Code 界面新增 GETS 和 HEADS 请求方式
1 parent 017e19c commit 9c58574

10 files changed

Lines changed: 269 additions & 95 deletions

File tree

‎APIJSON-Android/APIJSONTest/app/src/main/AndroidManifest.xml‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@
3232
</activity>
3333
<activity
3434
android:name="apijson.demo.ui.RequestActivity"
35-
android:label="@string/request" />
35+
android:label="@string/request"
36+
/>
3637
<activity
3738
android:name="apijson.demo.ui.AutoActivity"
3839
android:label="@string/auto"
39-
android:screenOrientation="landscape"
4040
android:windowSoftInputMode="adjustPan"
41+
android:configChanges="orientation|screenSize"
42+
android:screenOrientation="userLandscape"
4143
/>
4244
<activity
4345
android:name="apijson.demo.ui.UnitActivity"
4446
android:label="@string/unit"
45-
android:screenOrientation="landscape"
4647
android:windowSoftInputMode="adjustPan"
48+
android:configChanges="orientation|screenSize"
49+
android:screenOrientation="userLandscape"
4750
/>
4851
</application>
4952

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/AutoActivity.java‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.content.Context;
2020
import android.content.Intent;
2121
import android.content.SharedPreferences;
22+
import android.content.pm.ActivityInfo;
23+
import android.content.res.Configuration;
2224
import android.os.Bundle;
2325
import android.util.Log;
2426
import android.view.View;
@@ -56,10 +58,15 @@ public static Intent createIntent(Context context) {
5658

5759
private TextView tvAutoRequest;
5860
private TextView tvAutoResponse;
61+
62+
private TextView tvAutoCodePortrait;
63+
private TextView tvAutoCodeLandscape;
64+
private TextView tvAutoOrient;
5965
@Override
6066
protected void onCreate(Bundle savedInstanceState) {
6167
super.onCreate(savedInstanceState);
6268
requestWindowFeature(Window.FEATURE_NO_TITLE);
69+
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
6370
setContentView(R.layout.auto_activity);
6471
context = this;
6572

@@ -75,16 +82,43 @@ protected void onCreate(Bundle savedInstanceState) {
7582
}
7683

7784

85+
tvAutoRequest = findViewById(R.id.tvAutoRequest);
86+
tvAutoResponse = findViewById(R.id.tvAutoResponse);
7887

79-
tvAutoRequest = (TextView) findViewById(R.id.tvAutoRequest);
80-
tvAutoResponse = (TextView) findViewById(R.id.tvAutoResponse);
88+
tvAutoCodePortrait = findViewById(R.id.tvAutoCodePortrait);
89+
tvAutoCodeLandscape = findViewById(R.id.tvAutoCodeLandscape);
90+
tvAutoOrient = findViewById(R.id.tvAutoOrient);
8191

92+
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
93+
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
8294

8395

84-
tvAutoRequest.setText(StringUtil.getString(JSON.format(request)));
96+
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
97+
@Override
98+
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
99+
onConfigurationChanged(getResources().getConfiguration());
100+
}
101+
});
102+
103+
}
104+
105+
@Override
106+
protected void onResume() {
107+
super.onResume();
108+
onConfigurationChanged(getResources().getConfiguration());
109+
}
85110

111+
@Override
112+
public void onConfigurationChanged(Configuration newConfig) {
113+
onScreenChange(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
114+
super.onConfigurationChanged(newConfig);
86115
}
87116

117+
private void onScreenChange(boolean isHorizontal) {
118+
tvAutoCodePortrait.setVisibility(! isHorizontal ? View.VISIBLE : View.GONE);
119+
tvAutoCodeLandscape.setVisibility(isHorizontal ? View.VISIBLE : View.GONE);
120+
tvAutoOrient.setText(getString(R.string.screen) + getString(isHorizontal ? R.string.horizontal : R.string.vertical));
121+
}
88122

89123
public void copy(View v) {
90124
StringUtil.copyText(context, StringUtil.getString((TextView) v));
@@ -100,6 +134,12 @@ public void get(View v) {
100134
public void head(View v) {
101135
request((TextView) v);
102136
}
137+
public void gets(View v) {
138+
request((TextView) v);
139+
}
140+
public void heads(View v) {
141+
request((TextView) v);
142+
}
103143
public void post(View v) {
104144
request((TextView) v);
105145
}
@@ -110,6 +150,11 @@ public void delete(View v) {
110150
request((TextView) v);
111151
}
112152

153+
public void orient(View v) {
154+
setRequestedOrientation(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
155+
? ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
156+
}
157+
113158

114159

115160

@@ -121,7 +166,7 @@ public void auto(String request) {
121166
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n request = \n" + request + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
122167
Log.d(TAG, "\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n response = \n" + response + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
123168

124-
tvAutoResponse.setText(StringUtil.getString(response));
169+
tvAutoResponse.setText(StringUtil.getTrimedString(response));
125170
}
126171

127172

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/RequestActivity.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static Intent createIntent(Context context, long id, String url, String m
105105
@Override
106106
protected void onCreate(Bundle savedInstanceState) {
107107
super.onCreate(savedInstanceState);
108+
//FIXME 也解决不了竖屏只显示一半 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); // AndroidManifest 设置不兼容小米 MIX 3 Android 9.0
108109
setContentView(R.layout.request_activity);
109110
context = this;
110111
isAlive = true;
@@ -118,15 +119,15 @@ protected void onCreate(Bundle savedInstanceState) {
118119
method = StringUtil.getTrimedString(method);
119120
url = StringUtil.getCorrectUrl(url);
120121

121-
tvRequestResult = (TextView) findViewById(R.id.tvRequestResult);
122-
pbRequest = (ProgressBar) findViewById(R.id.pbRequest);
123-
etRequestUrl = (EditText) findViewById(R.id.etRequestUrl);
124-
btnRequestRequest = (Button) findViewById(R.id.btnRequestRequest);
122+
tvRequestResult = findViewById(R.id.tvRequestResult);
123+
pbRequest = findViewById(R.id.pbRequest);
124+
etRequestUrl = findViewById(R.id.etRequestUrl);
125+
btnRequestRequest = findViewById(R.id.btnRequestRequest);
125126

126127

127128

128129
etRequestUrl.setText(StringUtil.getString(StringUtil.isNotEmpty(url, true)
129-
? url : "http://apijson.cn:8080/"));//TODO 把这个ip地址改成你自己服务器的
130+
? url : "http://apijson.cn:8080/")); // TODO 把这个ip地址改成你自己服务器的
130131
btnRequestRequest.setText(method);
131132

132133
error = String.format(getResources().getString(R.string.request_error), StringUtil.getTrimedString(btnRequestRequest));

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/SelectActivity.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ protected void onCreate(Bundle savedInstanceState) {
7171
method = sp.getString(KEY_METHOD, null);
7272
name = sp.getString(KEY_NAME, null);
7373

74-
tvSelectLogin = (TextView) findViewById(R.id.tvSelectLogin);
74+
tvSelectLogin = findViewById(R.id.tvSelectLogin);
7575

7676
buttons = new Button[10];
77-
buttons[0] = (Button) findViewById(R.id.btnSelectPost);
78-
buttons[1] = (Button) findViewById(R.id.btnSelectPut);
79-
buttons[2] = (Button) findViewById(R.id.btnSelectDelete);
80-
buttons[3] = (Button) findViewById(R.id.btnSelectSingle);
81-
buttons[4] = (Button) findViewById(R.id.btnSelectColumns);
82-
buttons[5] = (Button) findViewById(R.id.btnSelectRely);
83-
buttons[6] = (Button) findViewById(R.id.btnSelectArray);
84-
buttons[7] = (Button) findViewById(R.id.btnSelectComplex);
85-
buttons[8] = (Button) findViewById(R.id.btnSelectAccessError);
86-
buttons[9] = (Button) findViewById(R.id.btnSelectAccessPermitted);
77+
buttons[0] = findViewById(R.id.btnSelectPost);
78+
buttons[1] = findViewById(R.id.btnSelectPut);
79+
buttons[2] = findViewById(R.id.btnSelectDelete);
80+
buttons[3] = findViewById(R.id.btnSelectSingle);
81+
buttons[4] = findViewById(R.id.btnSelectColumns);
82+
buttons[5] = findViewById(R.id.btnSelectRely);
83+
buttons[6] = findViewById(R.id.btnSelectArray);
84+
buttons[7] = findViewById(R.id.btnSelectComplex);
85+
buttons[8] = findViewById(R.id.btnSelectAccessError);
86+
buttons[9] = findViewById(R.id.btnSelectAccessPermitted);
8787

8888

8989
setRequest();

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/UnitActivity.java‎

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.content.Intent;
6+
import android.content.pm.ActivityInfo;
7+
import android.content.res.Configuration;
68
import android.os.Bundle;
79
import android.text.TextUtils;
810
import android.view.View;
11+
import android.view.Window;
912
import android.widget.TextView;
1013

1114
import com.koushikdutta.async.AsyncServer;
@@ -41,51 +44,89 @@ public static Intent createIntent(Context context) {
4144
private Activity context;
4245
private boolean isAlive;
4346

44-
private TextView etUnitPort;
4547
private TextView tvUnitRequest;
4648
private TextView tvUnitResponse;
49+
50+
private TextView tvUnitOrient;
51+
private TextView etUnitPort;
52+
private View pbUnit;
4753
@Override
4854
protected void onCreate(Bundle savedInstanceState) {
4955
super.onCreate(savedInstanceState);
56+
requestWindowFeature(Window.FEATURE_NO_TITLE);
5057
setContentView(R.layout.unit_activity);
5158
context = this;
5259
isAlive = true;
5360

54-
etUnitPort = findViewById(R.id.etUnitPort);
61+
5562
tvUnitRequest = findViewById(R.id.tvUnitRequest);
5663
tvUnitResponse = findViewById(R.id.tvUnitResponse);
64+
65+
tvUnitOrient = findViewById(R.id.tvUnitOrient);
66+
etUnitPort = findViewById(R.id.etUnitPort);
67+
pbUnit = findViewById(R.id.pbUnit);
68+
5769
etUnitPort.setText(port);
70+
pbUnit.setVisibility(View.GONE);
71+
72+
73+
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
74+
@Override
75+
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
76+
onConfigurationChanged(getResources().getConfiguration());
77+
}
78+
});
79+
80+
}
81+
82+
@Override
83+
protected void onResume() {
84+
super.onResume();
85+
onConfigurationChanged(getResources().getConfiguration());
86+
}
87+
88+
@Override
89+
public void onConfigurationChanged(Configuration newConfig) {
90+
tvUnitOrient.setText(
91+
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
92+
? (getString(R.string.screen) + getString(R.string.horizontal))
93+
: getString(R.string.vertical)
94+
);
95+
super.onConfigurationChanged(newConfig);
96+
}
97+
98+
public void copy(View v) {
99+
StringUtil.copyText(context, StringUtil.getString((TextView) v));
100+
}
101+
102+
public void orient(View v) {
103+
setRequestedOrientation(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
104+
? ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
58105
}
59106

60107

61108
private String port = "8080";
62109
public void start(View v) {
63110
v.setEnabled(false);
64111
port = StringUtil.getString(etUnitPort);
112+
startServer(Integer.valueOf(port));
65113

66-
etUnitPort.setText(port + " is starting...");
67-
startServer();
68-
69-
etUnitPort.setText(port + " is listening...");
70-
etUnitPort.setEnabled(false);
71114
v.setEnabled(true);
115+
etUnitPort.setEnabled(false);
116+
pbUnit.setVisibility(View.VISIBLE);
72117
}
73118
public void stop(View v) {
74119
v.setEnabled(false);
75120
server.stop();
76121
mAsyncServer.stop();
77122

78-
etUnitPort.setText(port);
79-
etUnitPort.setEnabled(true);
80123
v.setEnabled(true);
124+
etUnitPort.setEnabled(true);
125+
pbUnit.setVisibility(View.GONE);
81126
}
82127

83128

84-
public void copy(View v) {
85-
StringUtil.copyText(context, StringUtil.getString((TextView) v));
86-
}
87-
88-
private void startServer() {
129+
private void startServer(int port) {
89130
// server.addAction("OPTIONS","*", this);
90131
// server.get("/test", new HttpServerRequestCallback() {
91132
// @Override
@@ -99,7 +140,7 @@ private void startServer() {
99140
server.get("[\\d\\D]*", this);
100141
// server.post("/get", this);
101142
server.post("[\\d\\D]*", this);
102-
server.listen(mAsyncServer, Integer.valueOf(port));
143+
server.listen(mAsyncServer, port);
103144

104145
}
105146

0 commit comments

Comments
 (0)