|
| 1 | +package apijson.demo.ui; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.text.TextUtils; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import com.koushikdutta.async.AsyncServer; |
| 12 | +import com.koushikdutta.async.http.Headers; |
| 13 | +import com.koushikdutta.async.http.body.AsyncHttpRequestBody; |
| 14 | +import com.koushikdutta.async.http.server.AsyncHttpServer; |
| 15 | +import com.koushikdutta.async.http.server.AsyncHttpServerRequest; |
| 16 | +import com.koushikdutta.async.http.server.AsyncHttpServerResponse; |
| 17 | +import com.koushikdutta.async.http.server.HttpServerRequestCallback; |
| 18 | + |
| 19 | +import org.json.JSONException; |
| 20 | +import org.json.JSONObject; |
| 21 | + |
| 22 | +import apijson.demo.R; |
| 23 | +import apijson.demo.StringUtil; |
| 24 | + |
| 25 | + |
| 26 | +public class UnitActivity extends Activity implements HttpServerRequestCallback { |
| 27 | + private static final String TAG = "UnitActivity"; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param context |
| 31 | + * @return |
| 32 | + */ |
| 33 | + public static Intent createIntent(Context context) { |
| 34 | + return new Intent(context, UnitActivity.class); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + private AsyncHttpServer server = new AsyncHttpServer(); |
| 39 | + private AsyncServer mAsyncServer = new AsyncServer(); |
| 40 | + |
| 41 | + private Activity context; |
| 42 | + private boolean isAlive; |
| 43 | + |
| 44 | + private TextView etUnitPort; |
| 45 | + private TextView tvUnitRequest; |
| 46 | + private TextView tvUnitResponse; |
| 47 | + @Override |
| 48 | + protected void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + setContentView(R.layout.unit_activity); |
| 51 | + context = this; |
| 52 | + isAlive = true; |
| 53 | + |
| 54 | + etUnitPort = findViewById(R.id.etUnitPort); |
| 55 | + tvUnitRequest = findViewById(R.id.tvUnitRequest); |
| 56 | + tvUnitResponse = findViewById(R.id.tvUnitResponse); |
| 57 | + etUnitPort.setText(port); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + private String port = "8080"; |
| 62 | + public void start(View v) { |
| 63 | + v.setEnabled(false); |
| 64 | + port = StringUtil.getString(etUnitPort); |
| 65 | + |
| 66 | + etUnitPort.setText(port + " is starting..."); |
| 67 | + startServer(); |
| 68 | + |
| 69 | + etUnitPort.setText(port + " is listening..."); |
| 70 | + etUnitPort.setEnabled(false); |
| 71 | + v.setEnabled(true); |
| 72 | + } |
| 73 | + public void stop(View v) { |
| 74 | + v.setEnabled(false); |
| 75 | + server.stop(); |
| 76 | + mAsyncServer.stop(); |
| 77 | + |
| 78 | + etUnitPort.setText(port); |
| 79 | + etUnitPort.setEnabled(true); |
| 80 | + v.setEnabled(true); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + public void copy(View v) { |
| 85 | + StringUtil.copyText(context, StringUtil.getString((TextView) v)); |
| 86 | + } |
| 87 | + |
| 88 | + private void startServer() { |
| 89 | +// server.addAction("OPTIONS","*", this); |
| 90 | +// server.get("/test", new HttpServerRequestCallback() { |
| 91 | +// @Override |
| 92 | +// public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { |
| 93 | +// response.send("{\"hello\": \"world!\"}"); |
| 94 | +// } |
| 95 | +// }); |
| 96 | + |
| 97 | +// server.post("/get", this); |
| 98 | + server.addAction("OPTIONS", "[\\d\\D]*", this); |
| 99 | + server.get("[\\d\\D]*", this); |
| 100 | +// server.post("/get", this); |
| 101 | + server.post("[\\d\\D]*", this); |
| 102 | + server.listen(mAsyncServer, Integer.valueOf(port)); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onRequest(final AsyncHttpServerRequest asyncHttpServerRequest, final AsyncHttpServerResponse asyncHttpServerResponse) { |
| 108 | + runOnUiThread(new Runnable() { |
| 109 | + |
| 110 | + @Override |
| 111 | + public void run() { |
| 112 | + if (isAlive) { |
| 113 | + tvUnitRequest.setText(StringUtil.getString(asyncHttpServerRequest) + "\n\n\n\n\n" + StringUtil.getString(tvUnitRequest)); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + AsyncHttpRequestBody requestBody = asyncHttpServerRequest.getBody(); |
| 119 | + |
| 120 | + Headers allHeaders = asyncHttpServerResponse.getHeaders(); |
| 121 | + Headers reqHeaders = asyncHttpServerRequest.getHeaders(); |
| 122 | + |
| 123 | + String corsHeaders = reqHeaders.get("access-control-request-headers"); |
| 124 | + String corsMethod = reqHeaders.get("access-control-request-method"); |
| 125 | + |
| 126 | +// if ("OPTIONS".toLowerCase().equals(asyncHttpServerRequest.getMethod().toLowerCase())) { |
| 127 | + |
| 128 | + String origin = reqHeaders.get("origin"); |
| 129 | + String cookie = reqHeaders.get("cookie"); |
| 130 | + |
| 131 | + allHeaders.set("Access-Control-Allow-Origin", TextUtils.isEmpty(origin) ? "*" : origin); |
| 132 | + allHeaders.set("Access-Control-Allow-Credentials", "true"); |
| 133 | + allHeaders.set("Access-Control-Allow-Headers", TextUtils.isEmpty(corsHeaders) ? "*" : corsHeaders); |
| 134 | + allHeaders.set("Access-Control-Allow-Methods", TextUtils.isEmpty(corsMethod) ? "*" : corsMethod); |
| 135 | + allHeaders.set("Access-Control-Max-Age", "86400"); |
| 136 | + if (TextUtils.isEmpty(cookie) == false) { |
| 137 | + allHeaders.set("Set-Cookie", cookie + System.currentTimeMillis()); |
| 138 | + } |
| 139 | +// } |
| 140 | + |
| 141 | + try { |
| 142 | + JSONObject obj = new JSONObject(); |
| 143 | + obj.put("code", 200); |
| 144 | + obj.put("msg", "success"); |
| 145 | + asyncHttpServerResponse.code(200); |
| 146 | + |
| 147 | + switch (asyncHttpServerRequest.getPath()) { |
| 148 | + case "/invokeMethod": |
| 149 | + obj.put("api", "/invokeMethod"); |
| 150 | + break; |
| 151 | + case "/listMethod": |
| 152 | + obj.put("api", "/listMethod"); |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + asyncHttpServerResponse.send("OPTIONS".toLowerCase().equals(asyncHttpServerRequest.getMethod().toLowerCase()) ? new JSONObject() : obj); |
| 157 | +// asyncHttpServerResponse.send("application/json; charset=utf-8", obj.toString()); |
| 158 | + |
| 159 | + runOnUiThread(new Runnable() { |
| 160 | + |
| 161 | + @Override |
| 162 | + public void run() { |
| 163 | + if (isAlive) { |
| 164 | + tvUnitResponse.setText(StringUtil.getString(asyncHttpServerResponse) + "\n\n\n\n\n" + StringUtil.getString(tvUnitResponse)); |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + } catch (JSONException e) { |
| 169 | + e.printStackTrace(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + protected void onDestroy() { |
| 175 | + isAlive = false; |
| 176 | + super.onDestroy(); |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | +} |
0 commit comments