帰宅後 (端末側
検討着手。とりあえず GAE に JSON なリクエスト送るサンプルを漁る。
public ArrayList<String> getServerData() throws JSONException, ClientProtocolException, IOException { ArrayList<String> stringData = new ArrayList<String>(); DefaultHttpClient httpClient = new DefaultHttpClient(); ResponseHandler <String> resonseHandler = new BasicResponseHandler(); HttpPost postMethod = new HttpPost(URL_TO_PHP_FILE_TO_HANDLE_POST); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); JSONObject jsonObject = new JSONObject(); //Data being sent to the server, which should produce a reply jsonObject.put("someKey", someData); nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString())); postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs)); String response = httpClient.execute(postMethod,resonseHandler); JSONObject jsonResponse = new JSONObject(response); JSONArray serverData1 = jsonResponse.getJSONArray("data1"); JSONArray serverData2 = jsonResponse.getJSONArray("data2"); for(int i = 0; i < serverData1.length() && i < serverData2.length(); i++) { //Do something with the data } return //the data; }
ちょっと色々調べる必要あり。
ちなみに上記のコードは
から頂戴してます。あとレスポンスの取り扱いは以下からアイデア頂戴してます。
なんとなくコードを下書き。
public boolean putData() throws JSONException, ClientProtocolException, IOException { ArrayList<String> stringData = new ArrayList<String>(); DefaultHttpClient httpClient = new DefaultHttpClient(); ResponseHandler <String> resonseHandler = new BasicResponseHandler(); HttpPost postMethod = new HttpPost(URL_TO_PHP_FILE_TO_HANDLE_POST); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); Calendar cal = Calendar.getInstance(); JSONObject jsonObject = new JSONObject(); //Data being sent to the server, which should produce a reply jsonObject.put("id", "okinawa.jag"); // Google Account jsonObject.put("year", Integer.toString(cal.get(Calendar.YEAR))); jsonObject.put("month", Integer.toString(cal.get(Calendar.MONTH))); jsonObject.put("day", Integer.toString(cal.get(Calendar.DAY_OF_MONTH))); jsonObject.put("hour", Integer.toString(cal.get(Calendar.HOUR_OF_DAY))); jsonObject.put("minute", Integer.toString(cal.get(Calendar.MINUTE))); jsonObject.put("second", Integer.toString(cal.get(Calendar.SECOND))); nameValuePairs.add(new BasicNameValuePair("jsonString", jsonObject.toString())); postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs)); String response = httpClient.execute(postMethod,resonseHandler); int status = response1.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { return true; } return false; }
これで本当に良いのでしょうか。ちなみに上記手続きはあるイベント発生時に kick された thread から呼び出される予定ッス。
追記
年月日でバラしてナニするよりも System.currentTimeMillis() なソレを一発で送付して差し上げた方が良さげ。
epoc な数値を復活させるサンプルについては adakoda さんのサンプル有り。