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.
Related
I'd quickly explain what's going on in the JSON data above;
I have a table called messages, with some messages having a common message_id column. I grouped the messages by message_id. the boxes in red are the message_id's which have children
Now onto the question;
is it possible to access the children of the various arrays of message_id, without actually using the message_id string?
i.e iterate over the arrays
while (i < array.length) {
array[i]
}
if it's possible how can I do it?
Below is how I currently get the first array from the data object using the array id exactly
val jsonObject = JSONObject(response)
if (!jsonObject.getBoolean("error")) {
//getting data array from json response object
val dataObject = jsonObject.getJSONObject("data")
Log.i("MessageFragment", "[][] data array " + dataObject)
val array = dataObject.getJSONArray("NzbyxhmodN")
var i = 0
while (i < array.length()) {
//getting wallet object from json array
val message = array.getJSONObject(i)
//adding the wallet to wallet list
messageList!!.add(Message(
message.getInt("id"),
message.getInt("sender_id"),
message.getInt("receiver_id"),
message.getString("subject"),
message.getString("message"),
message.getString("message_id"),
message.getString("timestamp"),
message.getBoolean("isRead")
))
i++
}
I want to get the arrays without using the name i.e ("NzbyxhmodN")
Unfortunately, you cannot model without knowing the key value.
In such cases, I use this approach. It will be useful to you.
// data -> server json response
Iterator keys = data.keys();
while(keys.hasNext()) {
// random key
String key = (String)keys.next();
// and value...
JSONArray value = data.getJSONArray(key);
}
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);
}
I'm using Retrofit 2 and I need to send request body. The problem is somehow the value is converted to string. On the example below, you can see that items and totalPrice which should be array and number respectively are converted to string.
{ cashierId: 'fff7079c-3fc2-453e-99eb-287521feee63',
items: '[{"amount":3,"id":"602a79e3-b4c1-4161-a082-92202f92d1d6","name":"Play Station Portable","price":1500000.0}]',
paymentMethod: 'Debt',
totalPrice: '4500000.0' }
The desired request body is
{ cashierId: 'fff7079c-3fc2-453e-99eb-287521feee63',
items: [{"amount":3,"id":"602a79e3-b4c1-4161-a082-92202f92d1d6","name":"Play Station Portable","price":1500000.0}],
paymentMethod: 'Debt',
totalPrice: 4500000.0 }
Here's the service
#POST("api/sales")
#FormUrlEncoded
Call<Sale> createSale(
#FieldMap Map<String, Object> fields
);
And this is how I call createSale
Map<String, Object> fields = new HashMap<>();
fields.put("cashierId", UUID.fromString("fff7079c-3fc2-453e-99eb-287521feeaaa"));
fields.put("totalPrice", totalPrice);
fields.put("paymentMethod", paymentMethod);
fields.put("items", jsonArray);
Call<Sale> call = retailService.createSale(fields);
Is it possible to send those values as number and array, not as string?
The conversion most certainly happens because you are using #FormUrlEncoded.
According to the documentation:
Field names and values will be UTF-8 encoded before being URI-encoded in accordance to RFC-3986.
A solution would be to use a model class instead of a Map. I see you already have a Sale class. If it looks like something like this:
public class Sale {
String cashierId;
int totalPrice;
String paymentMethod;
ArrayList<SomeObject> items;
}
you can simply do like this:
// in service
#POST("api/sales")
Call<Sale> createSale(#Body Sale sale);
// when doing the call
Sale sale = new Sale();
// set everything in your object
// then
Call<Sale> call = retailService.createSale(sale);
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