Java tutorial
/** * Project Name:GizDataAccessSDKDemo * File Name:SaveActivity.java * Package Name:com.gizwits.gizdataaccesssdkdemo * Date:2015-1-12 15:30:30 * Copyright (c) 2014~2015 Xtreme Programming Group, Inc. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.gizwits.gizdataaccesssdkdemo.activitys; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.JSONArray; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.gizwits.gizdataaccess.GizDataAccessSource; import com.gizwits.gizdataaccess.entity.GizDataAccessErrorCode; import com.gizwits.gizdataaccess.listener.GizDataAccessSourceListener; import com.gizwits.gizdataaccesssdkdemo.Constant; import com.gizwits.gizdataaccesssdkdemo.R; import com.gizwits.gizdataaccesssdkdemo.utils.DateUtils; import com.gizwits.gizdataaccesssdkdemo.utils.NetworkUtils; // TODO: Auto-generated Javadoc /** * * ClassName: Class SaveActivity. <br/> * ?????json<br/> * date: 2015-1-9 18:26:04 <br/> * * @author Lien */ public class SaveActivity extends Activity { /** . */ EditText etYear; /** . */ EditText etMon; /** . */ EditText etDay; /** ?. */ EditText etHour; /** . */ EditText etMin; /** . */ EditText etSec; /** ?json. */ EditText etJson; /** ?. */ Button btnSave; EditText etTimes; private GizDataAccessSourceListener accessSourceListener = new GizDataAccessSourceListener() { /* * (non-Javadoc) * * @see com.gizwits.gizdataaccess.listener.GizDataAccessSourceListener# * didSaveData (com.gizwits.gizdataaccess.GizDataAccessSource, * com.gizwits.gizdataaccess.entity.GizDataAccessErrorCode, * java.lang.String) */ @Override public void didSaveData(GizDataAccessSource source, GizDataAccessErrorCode result, String message) { setProgressBarIndeterminateVisibility(false); if (result.getResult() == 0) { Toast.makeText(SaveActivity.this, "??", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(SaveActivity.this, ":" + message, Toast.LENGTH_SHORT).show(); } } }; /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_save); initViews(); } /** * ?. */ private void initViews() { etYear = (EditText) findViewById(R.id.etYear); etMon = (EditText) findViewById(R.id.etMon); etDay = (EditText) findViewById(R.id.etDay); etHour = (EditText) findViewById(R.id.etHour); etMin = (EditText) findViewById(R.id.etMin); etSec = (EditText) findViewById(R.id.etSec); etJson = (EditText) findViewById(R.id.etJson); etTimes = (EditText) findViewById(R.id.etTimes); btnSave = (Button) findViewById(R.id.btnSave); btnSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (NetworkUtils.isNetworkConnected(SaveActivity.this)) { saveData(); } else { Toast.makeText(SaveActivity.this, "", Toast.LENGTH_SHORT).show(); } } }); } /** * ?. */ private void saveData() { // ?? String strYear = etYear.getText().toString(); String strMon = etMon.getText().toString(); String strDay = etDay.getText().toString(); String strHour = etHour.getText().toString(); String strMin = etMin.getText().toString(); String strSec = etSec.getText().toString(); String strJson = replaceBlank(etJson.getText().toString().trim()); int repeatTimes = Integer.parseInt(etTimes.getText().toString()); // ? if (isEmpty(strYear) || isEmpty(strMon) || isEmpty(strDay) || isEmpty(strHour) || isEmpty(strMin) || isEmpty(strSec)) { Toast.makeText(SaveActivity.this, "", Toast.LENGTH_SHORT).show(); } else if (isEmpty(strJson)) { Toast.makeText(SaveActivity.this, "Json", Toast.LENGTH_SHORT).show(); } else if (repeatTimes <= 0) { Toast.makeText(SaveActivity.this, "0??", Toast.LENGTH_SHORT).show(); } else { // JSONObject jsonObject; // try { // ?json??JsonObject // jsonObject = new JSONObject(strJson); // ??1970 long time = DateUtils.getStringToDate(strYear + strMon + strDay + strHour + strMin + strSec + "000"); Log.i("saveData", "timelong=" + time); Log.i("saveData", "timestr=" + DateUtils.getDateToString(time)); strJson = "{\"attrs\":" + strJson + ",\"ts\":" + time + "}"; Log.i("saveData", "json=" + strJson); List<String> datas = new ArrayList<String>(); for (int i = 0; i < repeatTimes; i++) { datas.add(strJson); } // ? new GizDataAccessSource(accessSourceListener).saveData(Constant.TOKEN, Constant.PRODUCTKEY, Constant.DEVICE_SN, datas); // } catch (JSONException e) { // e.printStackTrace(); // Toast.makeText(SaveActivity.this, "json??,", // Toast.LENGTH_SHORT).show(); // } } } /** * ?null. * * @param str * * @return boolean true or false */ private static boolean isEmpty(String str) { if (str == null || str == "" || str.trim().equals("")) return true; return false; } /** * ? * * @param str * * @return String ?? * */ public static String replaceBlank(String str) { String dest = ""; if (str != null) { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(str); dest = m.replaceAll(""); } return dest; } }