I want to parse a string which is encoded in \u00 characters (like \u003e, \u003c etc) into normal string so that I can load it in webview. May I know how to parse it? I searched a lot on internet, but all results were php related.
EDIT: The string is here: http://pastie.org/8950227
I want to display it in webview, but it is displaying plain text.
Your data looks like it is a valid JSON string literal. You can use JSONTokener to parse it:
String data = ...;
String parsed = (String)new JSONTokener(data).nextValue();
Related
I have a Json String like below
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
What I want to do is send this Json String to a web server using the android studio volley library.
SO I keep getting this error
com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
The string cannot be converted to JsonArray.
So I have 2 questions I need help with.
My first is how to convert string to JsonArray. For example below String.
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
My second question is about passing this Jason Array (once we create) to a web server. I was researching about the Hashmap but I don't know how to send the JsonArray in Hashmap.
I had a go with below code, but it did not work.
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
I don't know how to send JsaonArray in hashmap.
So Overall, I would like to send this above string to web server.
Supose that the name of string is myString.
rString = myString.replace('[','');
rString = rString.replace(']','');
rString = rString.replace('"','');
String split[] = rString.split(',').
Now you have an array called split with every position is a day.
Edit: This is for frist question
Use that
JSONArray yourJsonArray = JSONArray.fromObject(yourJson);
Answer to first question:
I think it's not the exact response from the server, and there's a HTML <br> tag in the response.
because the line Value <br of type java.lang.String cannot be... clearly contains string <br, try replacing the HTML tags and/or decoding/unescaping the HTML tags to plain text, AND then you can convert that plain JSON text to JSONArray.
this snippet might help you out with decoding the response:
String parsedReponse = Html.fromHtml(serverResponse).toString()
if (parsedReponse != null) parsedReponse = parsedReponse.trim();
Answer to second question:
check out OkHttp3 and Retrofit libraries, they're pretty popular and amazing.
those can help you send POST data to your server
hope that helped you :)
i am working on a android project where i have to send a base64 converted image string to server and retrive but in my case the url is like below
i am able to convert the string but the string is too big and i am unable to pass that big string into the url. Is there any way i can pass this string to url as parameter?
I am trying to fetch json data from remote server. Everything works fine just when a two character string is passed my app gets force stopped. How to encode the url to prevent this error
here is how am trying to fetch json values
jsonobject = JSONfunctions.getJSONfromURL("http://www.example.com/index.php?sutyp="+sutyp);
If i pass "Hello" through sutyp there is no error but if i pass "Hello World" app force stops
How do I encode the url to prevent this. Any suggestions would be of great help
You need to use the android.net.Uri class:
String url = Uri.parse("http://www.example.com/index.php")
.buildUpon()
.appendQueryParameter("sutyp", sutyp)
.build()
.toString();
jsonobject = JSONfunctions.getJSONfromURL(url);
From the docs:
Encodes characters in the given string as '%'-escaped octets using the
UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers ("0-9"), and
unreserved characters ("_-!.~'()*") intact. Encodes all other
characters.
You could use URLEncoder.encode(string, "utf-8")
For example
String url = String.format("http://www.example.com/index.php?sutyp=%s", sutyp);
jsonobject = JSONfunctions.getJSONfromURL(URLEncoder.encode(url, "utf-8");
Here is a reference to the documentation
I have an XML web service. I want to parse this XML and I want to store in an separate textviews. The following is an XML content, and I have finished getting it in a String variable.
{
"30_year_rate": 4.25,
"30_year_pi": 196.78,
"30_year_apr": 4.375,
"20_year_rate": 4.375,
"20_year_pi": 250.37,
"20_year_apr": 4.5,
"15_year_rate": 3.75,
"15_year_pi": 290.89,
"15_year_apr": 3.875,
"10_year_rate": 3.625,
"10_year_pi": 397.89,
"10_year_apr": 3.75,
"5_year_arm": 2.75,
"5_year_pi": 163.3,
"5_year_apr": 2.875,
"7_year_arm": 3,
"7_year_pi": 168.64,
"7_year_apr": 3.125,
"3_year_arm": 4,
"3_year_pi": 190.97,
"3_year_apr": 4.125,
"5_year_io_arm": "N/A",
"5_year_io_pi": "N/A",
"5_year_io_apr": "N/A",
"QuoteID": 1449,
"Licensed": "N"
}
How can I parse this data? I want to convert it to a JSON object and retrieve it, or any other reasonable approach.
If what you're getting back from the webservice is the string above, then you already have a JSON string. To create an object that can retrieve information from it, use something like JSONObject.
JSONObject object = new JSONObject(your_string_variable);
double thirtyYearRate = object.getDouble("30_year_rate");
String licensed = object.getString("Licensed");
etc.
You might (will) run into some issues where you try to pull a double from a JSON field that contains a string; i.e., the "N/A" fields above. You'll likely have to pull them out as strings and then try to parse doubles from them, and if the parsing throws an exception, you'll know it's a string.
Alternately, you could look into JSON binding with something like Jackson, which apparently runs on Android.
To parse JSON, you could use the built in JSONObject org.json Or Json-lib if you use an old version of android.
To parse XML, use XMLPullParser. A sample can be found here: Parsing XML Data
i have this object in a Json file and i succeded at parsing many objects but i couldn't parse this one :
"advertiser_marker_geocoord": "33.848463,-7.033653"
how can i parse it using java?
// Here json is assumed to be a json object accompanied from response you would be getting.
String value = json.getString("advertiser_marker_geocoord");
now split values on behalf of , String arr[] = value.split(",");
If you want me share exact code.. please share the exact json that you are getting