How to send Base64 String as a parameter to api - android

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?

Related

How to convert ByteString to Image in Android?

I'm trying to convert from ByteString to Image once i receive the ByteString from Socket Connection.
A string which I received from Socket Connection onMessage
[size=302697hex=7c53657276657252657475726e4469723d54454d505f4449527c496d61676546696c654e616d653d496d6167655f3030312e6a70677c030905ffd8ffe000104a…]
How can I get the image from the above ByteString?
I tried to convert this string to Base64 URL, Byte Array but not worked for me.
Please help me to get out of this.
Thanks in Advance.
you need to first convert the hex string to byte array, then decode Base64 (not sure if required), then convert to image. If you can share a full sample string response, I check and verify the steps, and share the code.
you can use https://stackoverflow.com/a/18714790/11535103 to convert the hex string to byte array

how to pass value at runtime in url in android using json parsing?

i am trying to get name and address of hospitals,atm's,restaurants ....etc. near by my location with the help of google near by places api so i use json parsing ,since i can able to get all the name and address but only problem is that i can't understand that how can i get different values of ex. hotel,hospital,atm,bank...etc. using same url by putting different different values in run time.here is my url ....
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=26.841,75.801&radius=5000&types=hospital&key=AIzaSyBstND0mA7NZJPulZRcNtoWyJwXaKxqsFI";
In the above url there is a field "types" ,here i want to pass different different values at run time.....like hotel,hospital,bank,atm ...etc.
String type = "hospital";
int radius =5000;
String string = String.format("https://maps.googleapis.com/maps/api/place/nearbysearch
/json?location=26.841,75.801&radius=%d&types=%s&
key=AIzaSyBstND0mA7NZJPulZRcNtoWyJwXaKxqsFI", radius, type);
You can use Uri to build this
String base="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=26.841,75.801";
String RADIUS="5000";
String TYPES="hospital";
String KEYS="AIzaSyBstND0mA7NZJPulZRcNtoWyJwXaKxqsFI";
Uri builtUri=Uri.parse(base).buildUpon()
.appendQueryParameter("radius",RADIUS)
.appendQueryParameter("types",TYPES)
.appendQueryParameter("key",KEYS)
.build();
URL url=new URL(builtUri.toString());
OUTPUT
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=26.841,75.801&radius=5000&types=hospital&key=AIzaSyBstND0mA7NZJPulZRcNtoWyJwXaKxqsFI
You can easily change value of RADIUS,TYPES on runtime

encode url to fetch JSON data

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

Parse \u00 string android

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();

Send JSON from Android device to server not working

I have two links :
private static final String url_to_reg_device1 = "http://10.0.2.2/android_connec/regdevice.php";
private static final String url_to_reg_device2 = "http://www.something.in/form/regdevice.php";
One is for localhost and another is for server.
When I start activity, I create one JSON which store email and reg number and send it to server. On the server side, I decode JSON record and get values, store it in db.
The problem is that my code is working on local server. But when I upload it on server (to the URL in url_to_reg_device2), I got this error:
"Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject".
Check tags of JSON Object properly.

Categories

Resources