ThreadPoolExecutor (embedded in TaskManager) 管理threads的新增回收
- setCorePoolSize - available
- setMaximumPoolSize - max (2 x available)
- setKeepAliveTime - 10 secods (TimeUnit.SECONDS) - when thread number exceeding max, the idle ones are collected
- setRejectedExecutionHandler - 當workQueue滿了,池中的執行緒數又達到了maximumPoolSize,這個時候已經沒有多餘的資源處理請求了,需要RejectedExecutionHandler 來處理。
- execute - 執行command
WebGISListener 做為一個listener, 多了兩個聽到了要做的函式動作
- void onRegisterForItinerary(RegisterForItinerary data);
- void onGetItineraries(GetItineraryResult data);
MainActivity 在此成為一位WebGISListener
WebGISResponse 做為一個等待WEB動作的守門人,一旦結束動作立刻回報
- public void addListener(WebGISListener listener)
- public void removeListener(WebGISListener listener)
- public abstract void notifyListeners();
1. public class GetItineraryResult extends WebGISResponse
- abstract void parseJSON(JsonReader reader)
2. public class RegisterForItinerary extends WebGISResponse
- 負責註冊新的ID上到網址
public class WebGISClient {
public static final String TAG = "WebGISClient";
private static final String webgisUrl = "https://mapi.polstarapis.com/";
}
public static Runnable get(String uri, WebGISResponse result)
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
public static Runnable post(String uri, HashMap<String, String> params, WebGISResponse result)
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
WebGISClient 擔任interface角色提供MainActivity需要時建立的runnablerunnable透過TaskManager帶入執行,WebGISResponse帶入等結果,
一收到就NotifyListener,回傳給MainActivity做處理 :
private static void RequestItinerary() {
GetItineraryResult result = new GetItineraryResult();
Runnable r = WebGISClient.get("itinerary/api/getItinerary/" + SN, result);
result.addListener(MainActivity.mSingleton);
mTaskManager.execute(r);
}private static void UploadUserIdForItinerary(String userid) {HashMap<String, String> params = new HashMap<>();
params.put("serialNo", SN);
params.put("label", userid);
RegisterForItinerary result = new RegisterForItinerary();
Runnable r = WebGISClient.post("itinerary/api/register", params, result);
mTaskManager.execute(r);
}public void onGetItineraries(GetItineraryResult data) {
String toStr = String.format(Locale.getDefault(), "status=%d, message=%s", data.status, data.message);
if (data.status == -1) {
return;
}
int len = 0, i = 0;
for (ItineraryEntry e : data.itineraries) len++;
String[] titles = new String[len];
String[] subTitles = new String[len];
double[] longitudes = new double[len];
double[] latitudes = new double[len];
double[] navLongitudes = new double[len];
double[] navLatitudes = new double[len];
for (ItineraryEntry e : data.itineraries) {
titles[i] = e.title;
subTitles[i] = e.subTitle;
longitudes[i] = e.longitude;
latitudes[i] = e.longitude;
navLongitudes[i] = e.navLongitude;
navLatitudes[i] = e.navLatitude;
i++;
}
MainActivity.nativeSetItineraries(data.itineraryName, titles, subTitles, longitudes, latitudes, navLongitudes, navLatitudes);
}HTTPUrlConnection 可用的methods :Set the method for the URL request, one of:
- GET (用到)
- POST (用到)
- HEAD
- OPTIONS
- PUT
- DELETE
- TRACE