I've copied a snippet of a function I use in php to take an array and convert it into a sql where clause using the implode function. I would like to be able to repeat this function in Android Java code.
In other words pass a function a json array and have it do the equivalent of the implode function below to create the where clause in the query string. Does Java in Android have an "implode" equivalent or would anyone be able to help with an elegant solution to creating that where clause from a json array in Android Java? The json array would be of the form {"var1":"val1", "var2":"val2"}
// select from fact sighings - requires an array to do the where clause
public function select_from_fact_sighting($whereArray) {
$jsonArray = json_decode($whereArray, true);
$elementCount = count($jsonArray);
$where = array();
foreach($jsonArray as $row) {
foreach($row as $key => $val) {
$qval = $this -> quote($val);
$where[] = $key . " = " . $qval;
}
}
if (!empty($where))
$query = sprintf('SELECT * FROM fact_sightings WHERE %s', implode(' AND ', $where)');
Kotlin you convert your JSON objects into string list and pass like this to get the string form of the json with appended string
fun main(){
val arrayOrg = arrayOf("a","b","b","c","a","c","a")
println(arrayOrg.joinToString(" "))
}
//prints
a b b c a c a
in Java I guess this should work.
public static void main(String args[])
{
String gfg2 = String.join(" ", "select", "*", "from", "table", "where");
System.out.println(gfg2);
}
Related
I have a list of id's to search.
I want to call all data matching the values in this list.
Matching data is returned as a list.
I tried to do this in a loop and using IN but it didn't work.
When I write the values in parentheses one by one, it gives results, but how can I give the data in the list as a parameter?
db.query(_tableName, where: "id IN ('0001','00002','00003')")
When I type in the form, the query takes place. Can I send the incoming array directly into parentheses, not one by one? For example,
db.query(_tableName, where: "id IN (_ids)")
Query method:
Future<List<ContentModel>> getDiscoverContent(List<String> _ids) async {
Database db = await instance.database;
var contents = await db.query(_tableName, where: "id IN ('')");
List<ContentModel> _recommendedList = contents.isNotEmpty
? contents.map((e) => ContentModel.fromMap(e)).toList()
: [];
return _recommendedList;
}
The simplest solution I found:
var contents = await db.query(_tableName,
where:
"id IN('${_ids[0]}','${_ids[1]}','${_ids[2]}','${_ids[3]}','${_ids[4]}',"
"'${_ids[5]}','${_ids[6]}','${_ids[7]}','${_ids[8]}','${_ids[9]}')");
If you have a different solution, feel free to tell me.
You may try like this. This worked for my use case.
List<int> _ids = [1, 2, 3, 4];
// Loop through the _ids list and append quotes to each id
// add the element into a new List called new_Ids
List<String> new_Ids = [];
for (var i = 0; i < _ids.length; i++) {
new_Ids.add("'" + _ids[i].toString() + "'");
}
// use join to make a string from the new list where ids are separated by comma
String ids_Str = new_Ids.join(',');
Use db.query and pass ids string in where itself. Other columns in where condition can be passed in whereArgs if you need.
var resultList = await db.query('your_table_name',
columns: ['id', 'column2'],
where: 'id IN ($ids_Str)');
OR use rawQuery
var resultList = await db.rawQuery("SELECT id, column2 FROM your_table_name WHERE id IN(" + ids_Str + ")");
You may need to be careful of SQL Injection as we are passing dynamic values. Not sure about the best practices here.
How to send GET request with query params like this?
key1=["S","I","D"]&key2=[["A","X",0],["X","Y","Z"]]
According to retrofit java documents, you can do like below.
Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.
Array/Varargs Example:
#GET("/friends")
Call<ResponseBody> friends(#Query("group") String... groups);
Calling with foo.friends("coworker", "bowling") yields /friends??> group=coworker&group=bowling.
So you can do something like this
#GET("/something")
Call<ResponseBody> getSomething(#Query("key1") String[] key1,
Query("key2") String[] key2 );
foo.getSomething(key1, key2)
Update - above is the standard way to query parameters with multiple values
In order to send array as string, you can do like below
Parameter names and values are URL encoded by default. Specify encoded=true to change this behavior.
#GET("/something")
Call<ResponseBody> getSomething(#Query(value="key1", encoded=true) String key1);
Calling with foo.getSomething("['S','I','D']")) yields /something?key1=['S','I','D'].
Since retrofit doesn't recognize your custom array type, you have to consider parameters type String, then build your parameter like "["S","I","D"]" before pass to retrofit method.
For example create your parameter:
String parameter = "["
int arraySize = array.size()
for(int i = 0; i < arraySize; i++) {
parameter += "\"" + array.get(i) + "\"";
if(i != arraySize - 1) {//Append ',' except last element
parameter += ",";
}
}
parameter += "]";
Output for array with 'S','I' and 'D' elements is ["S","I","D"] String.
Now you can pass it as retrofit parameter.
I am looking for a way to insert JSON to SQLite.
I have a php script that looks like this.
<?php
require 'connection.php';
$var_fname = "Mildred";
$var_password = "ewan";
$var_email = "saf";
$var_username = "enanpogi";
$var_lastname = "Mildred";
$var_aviaryName = "Mildred";
$var_location = "myLocation";
$insert_stmt = $db->prepare(""
. "INSERT INTO users"
. "( fname, lname, email, password, username, aviary_name, location ) "
. "VALUES( :name, :lastname, :email, :password, :username, :aviaryName, :location)" );
// prepare and bind
$insert_stmt->execute(array(
':name' => $var_fname,
':lastname' => $var_lastname,
':password' => $var_password,
':email' => $var_email,
':username' => $var_username,
':aviaryName' => $var_aviaryName,
':location' => $var_location
));
$lastId = $db->lastInsertId();
if ($insert_stmt){
echo "inserted</br>";
echo $lastId;
//get all information of newly inserted and make json encode to prepare for insertion in sqlite_array_query
$stmt = $db->prepare('SELECT * '
. 'FROM users '
. 'WHERE id = :lastInsertedID ');
$stmt->bindParam(':lastInsertedID', $lastId);
$stmt->execute();
$results = $stmt->fetch(PDO::FETCH_ASSOC);
if($results > 0 ){
//echo sizeof($results);
echo "</br>";
echo json_encode(array("user_data"=>$results) );
}
}
?>
when I run that script, I was able to get below result
{"user_data":{"id":"28","fname":"Mildred","lname":"Mildred","email":"saf","username":"enanpogi","aviary_name":"Mildred","password":"ewan","location":"myLocation","phonenumber":null,"active":null}}
I am not sure if it is JSON Object or JSON array..
Now, I need help to insert that JSON to SQLite in my Android JAVA app.
Visit This Link : Json Viewer
Paste Your Output as a Text in this Link.
Click on Viewer.
You can find out that you got the json response or not.
I check your result.it's in json format.You already got the json Object.
Now, Use json_encode($results) function gives you a string so you can easily insert it database.
You will get JSON Object. json_encode will give you json object. But You will get json array when you use json_decode with second argument true.
I am using the bulk sms api to trigger send an sms to mobile number in and android phone. The format of the bulk sms api is follows
http://xxx.xxx.xxx/bulksms/bulksms?username=fds-xxx&password=xxx&type=0&dlr=1&destination=9422522891,8275004333&source=xxx&message=18%20December
The following is the response I can get in android as string using code
bytesSent = httpclient.execute(httppost, res);
response below
1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6
How do I parse this response like key value pairs ?
A little success with following code , but it fails when the regex char is '|'
public String[] split(String regex,String input)
{
input = "1701|919422522891:224c1214-bb95-414d-ba76-77db95370545,1701|918275004333:5e93a439-2644-4455-9f01-f27e6cf0cde6";
regex = "|"; // does not work
//regex = ":"; // works correct
String[] soso = Pattern.compile(regex).split(input, input.length());
for (String s : soso) {
Log.e("TAG",s.toString());
}
return null;
}
for regex char '|'
I get a Log output as single characters string array like {"1","7","0",........}
UPDATE
regex = "\\|" // works fine
Use the split() method of String to split the response into different entries.
Loop through the resulting array
use split() again to separate keys from values
store the result in a map, result[0] is the key,result[1] is the value
if you need to maintain order make sure you use a map that does that, e.g. LinkedHashMap
I want to parse my Json array dynamically. and want to get array of KEYS for each element under jsonarray. i an getting this through iterator. but not getting the sequeance as per the output json formate.
my JSON Formate :
{
"result": "Success",
"AlertDetails": [
{
"ShipmentNumber": "SHP34",
"Customer": "BEST",
"DateCreated": "2012-08-29T04:59:18Z"
"CustomerName": "BEST"
},
{
"ShipmentNumber": "SHP22",
"Customer": "BEST",
"DateCreated": "2012-08-29T05:34:18Z"
"CustomerName": "Jelly"
}
]
}
here is My Code :
JSONArray array = jsonobject.getJSONArray("AlertDetails");
JSONObject keyarray = array.getJSONObject(0);
Iterator temp = keyarray.keys();
while (temp.hasNext()) {
String curentkey = (String) temp.next();
KEYS.add(curentkey);
}
Log.d("Parsing Json class", " ---- KEYS---- " + KEYS);
What i am getting in logcate output:
---- KEYS---- [DateCreated,CustomerName, Customer, ShipmentNumber]
What i want :
---- KEYS---- [ShipmentNumber, Customer, DateCreated,CustomerName]
The JSONObject documentation (link: http://developer.android.com/reference/org/json/JSONObject.html) has the following description for the keys() function:
public Iterator keys ()
Since: API Level 1
Returns an iterator of the String names in this object. The returned
iterator supports remove, which will remove the corresponding mapping
from this object. If this object is modified after the iterator is
returned, the iterator's behavior is undefined. The order of the keys
is undefined.
So you may get the keys but the order is undefined. You may use any of the sorting algorithms if you want the keys in any particular order.
EDIT
Since you are unaware of the order of KEYS you are getting from the WS, after receiving the data you may show the details on screen in an ordered format . After building the arraylist KEYS, you may sort it alphabetically using the following:
Collections.sort(KEYS);
This will order the Strings in the KEYS arraylist according to its natural ordering (which is alphabetically).
I just come to know when I press ctlr+space bar, in which its clearly written that behavior of the keys is undefined, orders is not maintain by keys.
Arun George said# correctly that you have to use any sorting method to achieve your goal.
and for sorting may be this link will help you.
Use GSON library from google. It has a a lot of setting to read/create/parse json array and json objects. I didn't test it to find the solution, but I think it's very simple and full featured tool and can solve the problem.
Use different library to parse json dynamically.
Below I wrote a piece of code based on Jackson JSON Processor, which is the best JSON library in my opinion
public void test() throws IOException {
String str = "{\n" +
" \"result\": \"Success\",\n" +
" \"AlertDetails\": [\n" +
" {\n" +
" \"ShipmentNumber\": \"SHP34\",\n" +
" \"Customer\": \"BEST\",\n" +
" \"DateCreated\": \"2012-08-29T04:59:18Z\",\n" +
" \"CustomerName\": \"BEST\"\n" +
" }\n" +
" ]\n" +
"}";
JsonFactory factory = new JsonFactory();
JsonParser jsonParser = factory.createJsonParser(str);
JsonToken jsonToken;
SerializedString alertDetails = new SerializedString("AlertDetails");
while (!jsonParser.nextFieldName(alertDetails)) { /* move to AlertDetails field */ }
jsonParser.nextToken(); // skip [ start array
jsonParser.nextToken(); // skip { start object
// until } end object
while ((jsonToken = jsonParser.nextToken()) != JsonToken.END_OBJECT) {
if (jsonToken == JsonToken.FIELD_NAME) {
System.out.println(jsonParser.getCurrentName());
}
}
}
It simply prints out field names in the same order as in json:
ShipmentNumber
Customer
DateCreated
CustomerName
EDIT
Naturally you can use other libraries like gson etc. But remember, as is written on json.org, that:
An object is an unordered set of name/value pairs.
and the order of keys depends on implementation and might vary in each request.
There is also the method names();
Returns an array containing the string names in this object.
Edit: returns names in undefined order. Suggestions: parse it on your own