Improve json parse performance - android

I'd like to parse the json data coming from this api (taken from their example page):
https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=demo
I did build a parse method which takes ages (several seconds on my Galaxy A5) to parse the data (i know it is a lot of data to parse):
This is what i did:
private static List<PriceInfo> parsePriceDaily(JSONObject response, boolean onlyCurrentPrice) throws JSONException {
long millis = System.currentTimeMillis();
/* Check if this message is an Error Message = No data for symbol available.
* If not fetch data
* example structure
* https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=demo
*/
/* Get the First Object - Either Meta Data or Error Message */
Iterator<String> it = response.keys();
String key = it.next();
/* Is Error? */
if (key.contains("Error Message")) {
throw new JSONException(response.getString(key));
}
/* Valid Object - process metadata */
String timeZone = response.getJSONObject(key).getString("5. Time Zone");
DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
format.setTimeZone(TimeZone.getTimeZone(timeZone));
/* Process datasets */
List<PriceInfo> result = new ArrayList<>();
key = it.next();
JSONObject datasets = response.getJSONObject(key); /* Time Series (Daily) */
JSONObject dataset;
String dateString;
for (it = datasets.keys(); it.hasNext(); ) {
dateString = it.next(); /* dataset */
dataset = datasets.getJSONObject(dateString);
Date date = new Date(0); /* standard value */
try {
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
result.add(
new PriceInfo(
date,
dataset.getString("1. open"),
dataset.getString("2. high"),
dataset.getString("3. low"),
dataset.getString("4. close"),
dataset.getString("5. adjusted close"),
dataset.getString("6. volume"),
dataset.getString("7. dividend amount"),
dataset.getString("8. split coefficient")
)
);
if (onlyCurrentPrice) {
break;
}
}
Log.d(TAG, "Passed time: " + (System.currentTimeMillis() - millis));
return result;
}
What are the best improvements? Switching to another JSON library?

Use Gson for parsing and JsonSchemaToPojo for creating POJO classes .

Did you try gson library from Google?

Related

Custom deserializer for RealmObject

For learning purposes i'm creating an android app by using Realm and the Edinburg Festival Api. It's going pretty well except for one problem.
I'm using the following to convert the retrieved JSON to RealmObjects:
public void onResponse(final String response) {
realm.executeTransactionAsync(new Realm.Transaction(){
#Override
public void execute(Realm realm) {
// Update our realm with the results
parseImages();
realm.createOrUpdateAllFromJson(Festival.class, response);
}
}
}
This works fine except for one field, the images. The image part of the JSON:
"images": {
"031da8b4bad1360eddea87e8820615016878b183": {
"hash": "031da8b4bad1360eddea87e8820615016878b183",
"orientation": "landscape",
"type": "hero",
"versions": {
"large-1024": {
"height": 213,
"mime": "image/png",
"type": "large-1024",
}
"width": 1024
}
}
The problem here is the hash inside the image object. I have no clue how to handle this. The hash is different for every festival. Would it be possible to to make a custom JSON deserializer in my RealmObject?
Last code sample is my current model:
public class Festival extends RealmObject {
#PrimaryKey
public String title;
RealmList<Image> images;
public String description_teaser;
public String description;
public String genre;
public String age_category;
public String website;
public RealmList<Performance> performances;
public int votes;
}
I'm aware my PK is not optimal but this is still just testing to get the images working and i needed to set a PK for migrating.
Any tips are welcome, cheers :)
Update
Added the image model:
public class Image extends RealmObject {
public String hash;
public String orientation;
public String type;
RealmList<Version> versions;
}
Update 2
My attempt to parse the images before calling realm.createOrUpdateAllFromJson(Festival.class, response);
private void parseImages(String jsonString) throws JSONException {
JSONArray jsonArr = new JSONArray(jsonString);
for(int i = 0; i < jsonArr.length(); i++){
JSONObject jsonObj = jsonArr.getJSONObject(i);
JSONObject images = (JSONObject)jsonObj.get("images");
Iterator<String> iter = images.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject value = json.get(key);
realm.createOrUpdateObjectFromJson(Image.class,value);
} catch (JSONException e) {
// Something went wrong!
}
}
}
}
Update 3
I created a function that cleans up the broken JSON i get from the API. It ain't very nice but it works for now. it removes the hashes and the wierd versions and just places them both in a array. I'm sure it could be more efficiently written but i'll just go with this so i can move on with the rest of my app for now. See my own answer.
my own temporary solution:
/**
* Function to fix the json coming from the Festival API
* This is a bit more complicated then it needs to be but realm does not yet support #Serializedname
* It removes the "large-1024" (and simllar) object and places the versions in a JSON version array
* Then it removes the hashes and creates and images array. The JsonArray can now be parsed normally :)
*
* #param jsonString Result string from the festival api
* #return JSONArray The fixed JSON in the form of a JSONArray
* #throws JSONException
*/
private JSONArray cleanUpJson(String jsonString) throws JSONException {
JSONArray json = new JSONArray(jsonString);
for(int i = 0; i < json.length(); i++){
// We store the json Image Objects in here so we can remove the hashes
Map<String,JSONObject> images = new HashMap<>();
JSONObject festivalJson = json.getJSONObject(i);
JSONObject imagesJson = (JSONObject)festivalJson.get("images");
// Iterate each hash inside the images
Iterator<String> hashIter = imagesJson.keys();
while (hashIter.hasNext()) {
String key = hashIter.next();
try {
final JSONObject image = imagesJson.getJSONObject(key);
// Remove the version parents and map them to version
Map<String, JSONObject> versions = new HashMap<>();
JSONObject versionsJsonObject = image.getJSONObject("versions");
// Now iterate all the possible version and map add to the hashmap
Iterator<String> versionIter = versionsJsonObject.keys();
while(versionIter.hasNext()){
String currentVersion = versionIter.next();
versions.put(currentVersion,versionsJsonObject.getJSONObject(currentVersion));
}
// Use the hashmap to modify the json so we get an array of version
// This can't be done in the iterator because you will get concurrent error
image.remove("versions");
Iterator hashMapIter = versions.entrySet().iterator();
JSONArray versionJsonArray = new JSONArray();
while( hashMapIter.hasNext() ){
Map.Entry pair = (Map.Entry)hashMapIter.next();
versionJsonArray.put(pair.getValue());
}
image.put("versions",versionJsonArray);
Log.d(LOG_TAG,image.toString());
} catch (JSONException e) {
e.printStackTrace();
}
images.put(key,imagesJson.getJSONObject(key));
}
// Now let's get rid of the hashes
Iterator hashMapIter = images.entrySet().iterator();
JSONArray imagesJsonArray = new JSONArray();
while( hashMapIter.hasNext() ){
Map.Entry pair = (Map.Entry)hashMapIter.next();
imagesJsonArray.put(pair.getValue());
}
festivalJson.put("images", imagesJsonArray);
}
return json;
}
Hope it helps someone :) But sure ain't neat.
Due to how the keys are dynamic in this JSON (why isn't this an array? Whoever designed this API had no idea what they were doing), you'll have to manually parse the object up to the point of the hash key:
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject images = (JSONObject)jsonObj.get("images");
Iterator<String> iter = images.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject value = json.get(key);
realm.createOrUpdateObjectFromJson(Image.class, value.toString());
} catch (JSONException e) {
// Something went wrong!
}
}

Why do I get an empty response when my android app calls my API on my server?

I have android application that called information and show it as a list.
I have a spinner when you choose the date from the spinner you get the information related to that date.
In the app first load it calls automatically today information.
this is the code I use in my main activity to create my spinner and fill it with elements and handle the clicks on each item:
// Spinner element
spinner = (Spinner) v.findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// On selecting a spinner item
//String item = parent.getItemAtPosition(position).toString();
switch(position){
case 3:
if (JsonUtils.isNetworkAvailable(getActivity())) {
list.clear();
new MyTask().execute(Config.SERVER_URL + "/banko_api.php?d_o=-1");
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.failed_connect_network), Toast.LENGTH_SHORT).show();
}
break;
case 4:
if (JsonUtils.isNetworkAvailable(getActivity())) {
list.clear();
new MyTask().execute(Config.SERVER_URL + "/banko_api.php?d_o=0");
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.failed_connect_network), Toast.LENGTH_SHORT).show();
}
break;
case 5:
if (JsonUtils.isNetworkAvailable(getActivity())) {
list.clear();
new MyTask().execute(Config.SERVER_URL + "/banko_api.php?d_o=1");
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.failed_connect_network), Toast.LENGTH_SHORT).show();
}
break;
default:
if (JsonUtils.isNetworkAvailable(getActivity())) {
list.clear();
new MyTask().execute(Config.SERVER_URL + "/banko_api.php?d_o=0");
} else {
Toast.makeText(getActivity(), getResources().getString(R.string.failed_connect_network), Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Calendar calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date yesterday = calendar.getTime();
calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd/MM EEE");
String todayAsString = dateFormat.format(today);
String tomorrowAsString = dateFormat.format(tomorrow);
String yesterdayAsString = dateFormat.format(yesterday);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add(yesterdayAsString);
categories.add(todayAsString);
categories.add(tomorrowAsString);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, categories);
dataAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setSelection(4);
The problem : first load of the app is calling the data of today (which is the default choice in my spinner) without any problem.
if i choose another element in the spinner it also calls the related data without problem.
now if I want to select back today element in the spinner no data will be brought from the server even when the app at the start up it calls data from the same link and get it.
I get this message in my log :
W/System.err: org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject
The onPostExcute of my Asynktask contains this code:
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != progressDialog && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (null == result || result.length() == 0) {
Toast.makeText(getActivity(), getResources().getString(R.string.failed_connect_network), Toast.LENGTH_SHORT).show();
} else {
try {
Log.d("resultTT",result);
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(JsonConfig.CATEGORY_ARRAY_NAME);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
ItemMatch objItem = new ItemMatch();
objItem.setMatchId(objJson.getString(JsonConfig.Match_ID));
objItem.setMatchTournamentName(objJson.getString(JsonConfig.Match_LEAGUE_NAME));
objItem.setMatchTime(objJson.getString(JsonConfig.Match_TIME));
objItem.setMatchStatus(objJson.getString(JsonConfig.Match_STATUS));
objItem.setMatchLocalTeamName(objJson.getString(JsonConfig.Match_LOCALTEAM_NAME));
objItem.setMatchVisitorTeamName(objJson.getString(JsonConfig.Match_VISITORTEAM_NAME));
objItem.setMatchLocalTeamGoals(objJson.getString(JsonConfig.Match_LOCALTEAM_GOALS));
objItem.setMatchVisitorTeamGoals(objJson.getString(JsonConfig.Match_VISITORTEAM_GOALS));
objItem.setMatchBestOddPercent(objJson.getString(JsonConfig.Match_BEST_ODD_PERCENT));
objItem.setMatchBestOddResult(objJson.getString(JsonConfig.Match_BEST_ODD_RESULT));
list.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int j = 0; j < list.size(); j++) {
object = list.get(j);
array_match_id.add(String.valueOf(object.getMatchId()));
str_match_id = array_match_id.toArray(str_match_id);
array_league_name.add(String.valueOf(object.getMatchTournamentName()));
str_league_name = array_league_name.toArray(str_league_name);
array_match_time.add(String.valueOf(object.getMatchTime()));
str_match_time = array_match_time.toArray(str_match_time);
array_match_status.add(String.valueOf(object.getMatchStatus()));
str_match_status = array_match_status.toArray(str_match_status);
array_match_localteam_name.add(object.getMatchLocalTeamName());
str_match_localteam_name = array_match_localteam_name.toArray(str_match_localteam_name);
array_match_visitorteam_name.add(object.getMatchVisitorTeamName());
str_match_visitorteam_name = array_match_visitorteam_name.toArray(str_match_visitorteam_name);
array_match_localteam_goals.add(object.getMatchLocalTeamGoals());
str_match_localteam_goals = array_match_localteam_goals.toArray(str_match_localteam_goals);
array_match_visitorteam_goals.add(object.getMatchVisitorTeamGoals());
str_match_visitorteam_goals = array_match_visitorteam_goals.toArray(str_match_visitorteam_goals);
array_match_best_odd_percent.add(object.getMatchBestOddPercent());
str_match_best_odd_percent = array_match_best_odd_percent.toArray(str_match_best_odd_percent);
array_match_best_odd_result.add(object.getMatchBestOddResult());
str_match_best_odd_result = array_match_best_odd_result.toArray(str_match_best_odd_result);
}
setAdapterToListView();
}
In the try section of this code u can see I make a log of the result to see what is coming from the server i just get this : D/resultTT: []
and as you see the try is inside the else section so in the if statement of this section i check if the result is null or empty ; but the code passes it and enter the else statement but still showing that the returned result array is empty.
I want some help to find the reason behind this empty returned array even it loads fine at the start up. why can not it get the information after I choose any element in the spinner and then come back to the default (today) element?
UPDATE : this is my php side-server api code
<?php
include_once ('includes/variables.php');
DEFINE ('DB_HOST', $host);
DEFINE ('DB_USER', $user);
DEFINE ('DB_PASSWORD', $pass);
DEFINE ('DB_NAME', $database);
$mysqli = #mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL');
#mysql_select_db (DB_NAME) OR die ('Could not select the database');
?>
<?php
mysql_query("SET NAMES 'utf8'");
$date_offset = mysql_real_escape_string($_GET[d_o]);
//$date_offset = 0;
if(empty($date_offset) || $date_offset == "0")
{
$date_offset_value = "0";
$query="SELECT a.*, m.match_id, m.match_time, m.en_tournament_name FROM app_banko a inner join matches_of_comments m on m.match_id = a.match_id where a.date_offset = $date_offset_value limit 20";
$resouter = mysql_query($query);
}
else
{
$date_offset_value = $date_offset;
$query="SELECT a.*, m.match_id, m.match_time, m.en_tournament_name FROM app_banko a inner join matches_of_comments m on m.match_id = a.match_id where a.date_offset = $date_offset_value limit 20";
$resouter = mysql_query($query);
}
$set = array();
$total_records = mysql_num_rows($resouter);
if($total_records >= 1){
while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
$set['NewsApp'][] = $link;
}
}
echo $val= str_replace('\\/', '/', json_encode($set));
?>
If you get an array in return when expecting an object, there might be something wrong with the request to the API. One way is to figure it out it set up Wireshark on the development machine to sniff and filter the traffic. Then you can see if your request is faulty.
It is possible that the value of the response argument from the onPostExecute method contains stringified JSONArray, not JSONObject.
You can always test this with:
try:
JSONArray jsonArray = new JSONArray(result);
catch(JSONException e) {
// String `result` is not an array. Parse it as a regular JSONObject.
}
Testing wheter string is an empty json array (depends on it's formatting, especially when it may contain some white characters) checking it's length might be a pretty bad idea.
It all depends how are determined an API endpoints that you are calling.
One more tip at the end. If you are planning to consume REST API I strongly recommend using:
Retrofit - which allows you to easily define interfaces to access your API,
GSON - to automatically convert responses for Java models.
Your result string is an empty array but not an empty string. The empty array is represented as the following string:
String result = "[]";
In that case result.length() is equal to 2.
When parsing JSON you need to know if the parsed object is of type Object or of type Array. The former one is wrapped with braces {}, the later one with square brackets [].
So the following line:
JSONObject mainJson = new JSONObject(result);
Should probably be:
JSONArray mainJson = new JSONArray(result);
But I cannot emphasize enough that you need to know what your API returns if you want to be able to parse it correctly.
EDIT:
Well, json_encode will have a hard time to guess whether it should create a JSON Array or a JSON Object out of the empty array that you created with $set = array();.
Adding objects to the array like you do in your loop makes it obvious for json_encode that it should create a JSON Object.
I don't know if you can force json_encode's behavior, but worst case you could check yourself if the array is empty and return "" or null if the array is empty.
$set = array();
$total_records = mysql_num_rows($resouter);
if ($total_records >= 1) {
while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)) {
$set['NewsApp'][] = $link;
}
echo $val= str_replace('\\/', '/', json_encode($set));
} else {
echo $val="";
}
please put a check result.isEmpty() in your try block condition may this could solve your problem.
you can not directly get response in string . it can use JSONObject and JSONArray.

Compare Two JSON object

I am having JSON object like
{
key1:value1,
key2:value2
key3:value3
}
and i will write this JSON content to file.(Done)
For next interval of time i am getting JSON Object as
{
key1:newValue1,
key2:value2
key3:newValue3
}
I need to find out difference between each values. and need to write new json into file
{
key1:(value1- newValue1),
key2:(value2 - value2)
key3:(value3- newValue3)
}
How can i achieve it.? Please help.
The code below just iterates through the keys of your testObject, subtracts the value of the object from file and puts the result of this subtraction back to the testObject... Than you can save, or do whatever you want to do with the values in testObject
for(Iterator<String> iterator = testObject.keys(); iterator.hasNext();) {
String key = iterator.next();
try {
int testValue = testObject.getInt(key);
int fileValue = fileObject.getInt(key);
int differenceValue = testValue - fileValue;
testObject.put(key, differenceValue);
} catch (JSONException e) {e.printStackTrace();}
}

Storing String Array as JSON Array then getting Ljava.lang.String# when trying to receive and set TextView as value

Right now i am pulling a value from a EditText MainActivity and putting that as at string in sharedpref. In a later activity I am receiving that value and trying to add it to the end of an array that is contunially growing. To store the Array in that later activity for receiving later to continue adding I used a JSON array. So basically I create a String Array when the counter is 0 (first input the user has done since clearing) and receive the string value from the MainActivity EditText and add that as the first value in the String Array and then store it as a JSON Array. When I receive it later (when counter is > 0) and convert it back to string Array, the outPut in my TextView for any given spot in the array is [L.java.lang.String;#42a5e438,. Below is my code for both if coutner == 0 and counter >0; Does anyone know what the solution is?
int placeCounterExplanation = pref.getInt("placeCounterExplanation",0);
//getting new item
String explanationItem = pref.getString("explanationItem",null);
if(placeCounterExplanation > 0){
///////////////////////////////////// RECEIVE//
//pull existing array
JSONArray explanationjArray;
try {
explanationjArray = new JSONArray(settings.getString("jArray", ""));
//converting from json back to workable string array
// String []ExplanationDetailArray = new String[explanationjArray.length()];
String []ExplanationDetailArray = new String[100];
//matching them/converting
for(int i = 0, count = explanationjArray.length(); i<= count; i++)
{
try {
String jsonString = explanationjArray.getString(i);
ExplanationDetailArray[i] = jsonString;
}
catch (JSONException e) {
e.printStackTrace();
}
}
//after matching^ it adds newest value
ExplanationDetailArray[placeCounterExplanation] = explanationItem;
////////////////////////////////////////////////////////////////////////
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
if (product.equals("Alcohol")){
test.setText(ExplanationDetailArray[0]);
String placeCount = Integer.toString(placeCounterExplanation);
test2.setText(ExplanationDetailArray[1]);
// test3.setText(placeCounterExplanation - 1);
}
//////////////////////////////////////////////////////////////////////////
/////STORE
explanationjArray.put(Arrays.toString(ExplanationDetailArray));
// explanationjArray.put(ExplanationDetailArray);
editor2.putString("jArray", explanationjArray.toString());
editor2.commit();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (placeCounterExplanation == 0){
ExplanationDetailArray = new String[100];
ExplanationDetailArray[0] = explanationItem;
JSONArray explanationjArray = new JSONArray();
//////////////////////////////
// String sC = Integer.toString(spotCounter);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
if (product.equals("Alcohol")){
test.setText(ExplanationDetailArray[0]);
// test2.setText(ExplanationDetailArray[1]);
// test3.setText(placeCounterExplanation - 1);
}
///////////////////////////////
//STORING MIGHT NOT NEED THIS INSIDE HERE
// explanationjArray.put(ExplanationDetailArray);
explanationjArray.put(Arrays.toString(ExplanationDetailArray));
editor2.putString("jArray", explanationjArray.toString());
editor2.commit();
// placeCounterExplanation = placeCounterExplanation + 1;
// editor.putInt("placeCounterExplanation",placeCounterExplanation);
//editor.commit();
}
//after either IF Statement catches teh program, it advances the counter.
placeCounterExplanation = placeCounterExplanation + 1;
editor.putInt("placeCounterExplanation",placeCounterExplanation);
editor.commit();
I believe your problem is coming from the following line in code :
explanationjArray.put(ExplanationDetailArray);
This will insert a textual representation of the object into the JSON array. Instead you can do :
explanationjArray.put(Arrays.toString(ExplanationDetailArray));
or your own implementation of converting the string array to a concatenated set of strings.

Ormlite query using date

public GenericRawResults<Object[]> getCountByStatus(Date date,int status){
Log.info("CallDayPlanningDao",date.toString());
GenericRawResults<Object[]> rawResults=null;
Dao callDayPlanningDao = getDao(CallDayPlanning.class);
QueryBuilder query = callDayPlanningDao.queryBuilder();
int year = date.getYear();
int month = date.getMonth();
Date date1 = new Date(year, month,1);
Date date2 = new Date(year, month+1,1);
Date startDate = new Date(date1.getTime()-5);
Date endDate = new Date(date2.getTime()-5);
try {
**query.where().between("calldate", startDate, endDate);**//This line is not working
if(status==Constant.cnStatus){
query.where().in("callstatus", status,Constant.ccStatus);
}else{
query.where().eq("callstatus", status);
}
query.groupBy("calldate");
query.selectRaw("calldate,count(*)");
rawResults = callDayPlanningDao.queryRaw(query.prepareStatementString(), new DataType[] {
DataType.DATE_STRING, DataType.INTEGER });
// page through the results
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return rawResults;
}
Well, I want to get the count of the object, but the condition of date is invalid, I get all the data from my database.Somebody could help me?Thanks.
I am new to ORMLite and faced the same problem when accessed SQLite database.
It took me a whole day today to figure it out, here is the summary:
I found format "yyyy-M-d H:m:s" works fine in ORMLite for dealing with SQLite DateTime data type, not ORMLite's default format "yyyy-MM-dd HH:mm:ss.SSSSS".
For ORMLite to translate between "Java Date" and "SQLite DateTime", a persister class will be needed.
Here shows the code of the persister class I use, which override the public functions of DateStringType and use "dateFormatConfig" instead of defaultDateFormatConfig" :
`
public class DateStringSQLiteType extends DateStringType {
protected static final DateStringFormatConfig dateFormatConfig = new DateStringFormatConfig(
"yyyy-M-d H:m:s");
private static final DateStringSQLiteType singleTon = new DateStringSQLiteType();
public static DateStringSQLiteType getSingleton() {
return singleTon;
}
private DateStringSQLiteType() {
super(SqlType.STRING, new Class<?>[0]);
}
/**
* Convert a default string object and return the appropriate argument to a
* SQL insert or update statement.
*/
#Override
public Object parseDefaultString(FieldType fieldType, String defaultStr)
throws SQLException {
DateStringFormatConfig formatConfig = convertDateStringConfig(
fieldType, dateFormatConfig);
try {
// we parse to make sure it works and then format it again
return normalizeDateString(formatConfig, defaultStr);
} catch (ParseException e) {
throw SqlExceptionUtil.create("Problems with field " + fieldType
+ " parsing default date-string '" + defaultStr
+ "' using '" + formatConfig + "'", e);
}
}
/**
* Return the SQL argument object extracted from the results associated with
* column in position columnPos. For example, if the type is a date-long
* then this will return a long value or null.
*
* #throws SQLException
* If there is a problem accessing the results data.
* #param fieldType
* Associated FieldType which may be null.
*/
#Override
public Object resultToSqlArg(FieldType fieldType, DatabaseResults results,
int columnPos) throws SQLException {
return results.getString(columnPos);
}
/**
* Return the object converted from the SQL arg to java. This takes the
* database representation and converts it into a Java object. For example,
* if the type is a date-long then this will take a long which is stored in
* the database and return a Date.
*
* #param fieldType
* Associated FieldType which may be null.
* #param sqlArg
* SQL argument converted with
* {#link #resultToSqlArg(FieldType, DatabaseResults, int)} which
* will not be null.
*/
#Override
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos)
throws SQLException {
String value = (String) sqlArg;
DateStringFormatConfig formatConfig = convertDateStringConfig(
fieldType, dateFormatConfig);
try {
return parseDateString(formatConfig, value);
} catch (ParseException e) {
throw SqlExceptionUtil.create("Problems with column " + columnPos
+ " parsing date-string '" + value + "' using '"
+ formatConfig + "'", e);
}
}
/**
* Convert a Java object and return the appropriate argument to a SQL insert
* or update statement.
*/
#Override
public Object javaToSqlArg(FieldType fieldType, Object obj) {
DateFormat dateFormat = convertDateStringConfig(fieldType,
dateFormatConfig).getDateFormat();
return dateFormat.format((Date) obj);
}
/**
* #throws SQLException
* If there are problems creating the config object. Needed for
* subclasses.
*/
#Override
public Object makeConfigObject(FieldType fieldType) {
String format = fieldType.getFormat();
if (format == null) {
return dateFormatConfig;
} else {
return new DateStringFormatConfig(format);
}
}
}
`
Define you data class with notation:
#DatabaseField(..., persisterClass = DateStringSQLiteType.class)
private Date date;
It worked fine for me, can do "Between" query like:
list = foo.getDao().queryBuilder().where().between(HistoryStandardView.DATE_FIELD_NAME, new Date(98,1,1), new Date(115,1,1)).query();
ORMLite's logger shows the resulting statement:
[DEBUG] StatementExecutor query of 'SELECT * FROM `HistoryStandardView` WHERE `date` BETWEEN '1998-2-1 0:0:0' AND '2015-2-1 0:0:0' ' returned 2 results
Correct me if am wrong is your calldate column's type is DataType.DATE_STRING ? If that's the case it means that the persisted data type is VARCHAR so when you execute your query your doing a String comparison and not a Date comparison. So to solve your problem you can either :
Change your calldate column's type to DataType.DATE which is represented as a TIMESTAMP.
Change your calldate column's type to DataType.DATE_LONG.
Find a way to do a String comparison that matches what your need (for instance calling the sql date(calldate) fonction if your calldate values matches a Time Strings format see http://www.sqlite.org/lang_datefunc.html).
Here is what i did, it's not pretty but works like wanted to:
QueryBuilder<OffreEntity, Integer> qb = this.daoOffre.queryBuilder();
//Need to format the date i want to compare so it can actually be compare with what i have on db
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyMMdd");
String correctFormat = dateFormatter.format(dateLimite);
//In db the date is represented as a VARCHAR with format dd/MM/yy so i need to reformat so it matches yyMMdd
String rawQuery = String.format("substr(%1$s,7)||substr(%1$s,4,2)||substr(%1$s,1,2) > '%2$s'", OffreEntity.COLUMN_NAME_DATE, correctFormat);
qb.where().raw(rawQuery);
offresDept = qb.query();
Hope it helps!
Ps: Thanks to Jack Douglas for the date format query

Categories

Resources