I am sending data (using Retrofit) to server like below way
"[\"emailaddress\"]"
But server need that data in below way
["emailaddress"]
I have tested in PostMan, only ["emailaddress"] works fine..
I have tried below code which is not giving me result what server wants.
var emailArray = "[\"$email\"]"
Can anyone help me how can i achieve that?
What should I need to change to make it as server wants?
Can you create json array object and put the content in it .. like below.
in Java this how you can acheive.
JSONArray postdata = new JSONArray();
postdata.put(<email address>);
and pass this to server by
postdata.toString();
So basically create array and push email to it, then while passing the data covert to string, that should work.
Escape the backslash in the regex.
Try this :
var emailArray = "[\"emailaddress\"]"
var emailArrayNew = emailArray.toString().replace("\\\\", "")
Could you try using List/ArrayList there?
val emailList = ArrayList<String>()
emailList.add("email1")
emailList.add("email2")
Then in Retrofit Api Service, do something like this
#Field("email") emailList:List<String>
Hope this will help you
Related
I am a fresher in developing Android apps, in my app I want to post params in the format below:
user_id=12&task_id=100&user_ids=["91","92"]
I am not able to send the user_ids in array format. I am receiving the user_ids as string in my Rails API.
I am using HttpURLConnection, and in my writer.write() I am passing my post params as
writer.write("user_id="+id+"&task_id="+tid+"&user_ids="+["91","92"]+");
Please help.
If any one facing the same problem, I found a work around.
You need to add the array in a string
1. Step 1
Create a final ArrayList<String> SelectedTagIds = new ArrayList<String>();
and add the array items using
SelectedTagIds.add("One");
SelectedTagIds.add("Two");
...
2. Step 2
Convert
SelectedTagIds.toString();
3. Step 3
Format the String by replacing the string to
SelectedTagIds.replace("[","").replace("]","").replaceAll("\\s","").trim();
4 Step 4
Finally post the String to the server (I used HttpURLConnection to post my data to the server), and in the server code, convert the received SelectedTagIds to Array and loop accordingly to achieve you'r desired result.
I used json_encode(); to convert string to json in php and then response it to android but I can't use the response, how can I convert the json to string?
when I display the response it shows this :
"{\n'OK': \n[\n{\n'Name': 'MyName',\n'Gender':'Male'\n}\n]\n}"
what shall I do?
thank you
Since you're just converting a string to json, you're not returning a JSONObject or JSONArray, according to: http://php.net/manual/en/function.json-decode.php
If you must return a string, you may have to use some json library or write your own parser.
If that doesn't sound appealing, I recommending returning a JSONObject or JSONArray with one element.
For example:
php
echo json_encode( array('result' => 'the string you are encoding') );
java
JSONObject json = new JSONObject( encodedStringResponseFromPhp );
String theStringYouEncoded = (String) json.get( "result" );
You'll need to add a throws JSONException to the function you add this java code too or put it inside a try catch block.
Have you tried using a JSON-Library like https://code.google.com/p/json-simple/? Looks like you need some help decoding the string.
Edit: You should use the json2.js library from Douglas Crockford. It provides some extra features and better/older browser support.
Read more...
im struggling with json again :(
Here is the original response:
{"xml-fragment":{"workItem":{"#id":"251","#version":"74"},"presentation":{"#formIdenitifier":"1.0.0.201310151421/openspaceGWTPull_DefaultChannel/.default/Npda/NpdaProcess/UserReconcile/UserReconcile.gwt.json","#type":"GWT_FORM","#version":"1.0.0.201310151421","#activityName":"UserReconcile"},"workTypeDetail":{"#typePiled":"false","#pilingLimit":"0","#uid":"WT__RIoPEDWTEeOr4-yR8gXd7g","#version":"1.0.0.201310151421"},"payloadModel":{"serializedPayload":"{items:[{\"$param\":\"BankReconInput\",\"mode\":\"IN\",\"$value\":[{\"bankAccountTx_pk\":\"55213\",\"amount\":\"10099\",\"reference\":\"ImAmReference\",\"date\":\"2013-10-15\",\"reconType\":\"?\",\"amxcaseref\":\"pvm:0a12iq\",\"$type\":\"coza.npda.bom.BankTransaction\"}]}]}","#payloadMode":"JSON"}}}
i want to for example get value of amount from the serializedPayload. The problem is that it is not a json object. If i try:
obj = new JSONObject(jsonResp).getJSONObject("xml-fragment").getJSONObject("payloadModel");
this returns to me serializedPayload as a string and #payloadMode as a string.
i tried:
obj = new JSONObject(jsonResp).getJSONObject("xml-fragment").getJSONObject("payloadModel").getJSONObject("serializedPayload");
its confirms that serializedPayload is not a json object.
I looked at this example: http://developer.android.com/reference/org/json/JSONTokener.html
But its data is not as complex as mine and i am struggling to find java examples of how to do this.
Please can anyone help.
You don't need an example, you need to look at the JSON and think for a second.
serializedPayload is not a JSON object to begin with, it's really a string that has another piece of json encoded inside, sort of like the russian nesting dolls (frankly, it's an abomination).
You need to take the string, and then parse it again, using another JSONObject, sort of:
String payload = data..getJSONObject("xml-fragment").getJSONObject("payloadModel").getString("serializedPayload");
JSONObject theRealData = new JSONObject(payload);
Hi friends i have a java.util.Map object in Ajax method like this..
Map<String,List<String>> sm = new TreeMap<>();
List<String> str = new ArrayList<String>();
str.add("val1");
str.add("val2");
str.add("val3");
str.add("val4");
sm.put("shift1", str);
sm.put("shift2", str);
sm.put("shift3", str);
sm.put("shift4", str);
JSONObject json = new JSONObject(sm);
response.getWriter().print(json);
In run time Map elements will be increase or decrease
according to map elements I have to generate table
This is Ajax call.. I don't know how to parse that Map object and dynamically generate the table in javascript.
Please show me how.
I am just answering your question. I don't know java and also i don't understand the details you gave above. If I am wrong, I am sorry and kindly ignore it.
To parse the string to object
In the jQuery, $.parseJSON(string);
In js, JSON.parse(string);
I hope this will help U. Thanks.
var json = $.parseJSON( jsonString );
// This one wouldn't be supported in old browsers
json = JSON.parse( jsonString );
Additionally, you could, in Java, respond with the Content-Type application/json and then in the jQuery AJAX you will receive the JSON already parsed for you.
Of course this will not happen if you specifically configure your jQuery AJAX call to receive an plain text, or HTML, or other content-type response.
For more reference, see here, at the dataType setting.
I'm trying to send a byte[] (using PUT) with Restlet but I can't find any info on how to do it. My code looks like this:
Request request = new Request(Method.PUT, url);
request.setEntity( WHAT DO I PUT HERE?, MediaType.APPLICATION_OCTET_STREAM);
I had expected to find something along the lines of ByteArrayRepresentation, just like there's a JsonRepresentation and a a StringRepresentation but I couldn't find anything.
I believe you want to use an InputRepresentation, like so:
Representation representation = new InputRepresentation(new ByteArrayInputStream(bytes), MediaType.APPLICATION_OCTET_STREAM);
request.setEntity(representation);
I'm not familiar with restlet, but one way to do it would be to base64 encode the data. Then you could handle it like a regular string.
you can try subclassing WritableRepresentation that is especially designed for large representations