convert byteArray to string to initialize jsonObject - android

i have byteArray.
is it possible to convert byteArray to String?
please check my code
byte[] data = **some_byte_array**
JSONObject jsonObject = new JSONObject(data);
how do i fix this.

Try this
String decoded = new String(bytes, "UTF-8");
There are a bunch of encodings you can use, look at the Charset class in the Sun javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
answer credit goes to https://stackoverflow.com/a/1536365/4211264

Yes you can convert byte array to String using one of the String constructors like this :
String myString = new String(yourByteArray);
Documentation for the same:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])
All the best :)

Related

Options for packing/unpacking several strings for transmission via a byte[] parameter

I've got several strings I want to send to a wearable app via the MessageApi.sendMessage method, which just takes a byte[] parameter to carry the payload.
I could concatenate all the strings together and introduce some arbitrary separating character sequence then convert the result to bytes and do the reverse to unpack it on the watch.
But I was wondering if there's a ready-made solution for such a thing in Android (or Java)?
As commented, Data*Stream are made for that:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream data = new DataOutputStream(byteStream);
data.writeUTF("Whatever string you have here");
data.writeUTF("Write any number of strings");
byte[] result = byteStream.toByteArray();
On the other side:
byte[] myBytes = ...; // Retrieve the bytes through MessageApi
ByteArrayInputStream byteStream = new ByteArrayInputStream(myBytes);
DataInputStream data = new DataInputStream(byteStream);
String str1 = data.readUTF();
// That would be "Whatever string you have here"
String str2 = data.readUTF();
// And "Write any number of strings"
That's that simple. Since those are byte[] back streams, there is no need to close them.

convert byte[] from json in android

how to get byte[] from json using webservice
My json format is given below:
[{"imgLogo":[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0,255,219,0,132,0,
9,6,7,20,19,18,20,20,19,20,21,22,20,22,20,21,24,22,21,21,20,20,20,20,20,
24,21,20,22,24,20,20,20,23,24,28,40,32,24,26,37,28,20,
21,33,49,33,37,41,43,46,46,46,23,31,51,56,51,44,55,40,45,46,43,1,10,10,10
,14,13,14,27,16,16,26,44,36,28,36,44,44,44,44,44,44,44,44,44,44,44,44,
44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,
44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,255,192,0,17,8,0,194,1,3,3....]}]
I'm using this following code to get json value like this :
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String imagestring = jsonChildNode.getString("imgLogo");
Now, I'm getting above byte[] value as string. then how can I convert this string to byte[]
first create a String containing your data and then
byte[] data = Base64.decode(iconString, Base64.DEFAULT);
now you have byte array containing your bitmap.
Use this getBytes method :
byte[] bytes = imagestring .getBytes("UTF-8");

Value of type java.lang.String cannot be converted to JSONObject in android

I am reading json file from assets folder in my project in android. I am trying to convert String returned after reading json file into JSONObject. I googled alot but can't find fault in my code. Others are having success using this code. Anyone, kindly guide me if my approach is wrong or some fault is in my code.
At line - //1 , i get the error stated in title.
AssetManager am = getAssets();
InputStream is;
try
{
is=am.open("places.json");
int size = is.available();
byte[] buffer = new byte[size];
is.close();
String bufferString = new String(buffer);
JSONObject jo = new JSONObject(bufferString); //1
JSONObject pjo = jo.getJSONObject("Places");
You should consider using JSONReader:
http://developer.android.com/reference/android/util/JsonReader.html
Available from API level 11.
But I'm sure it's also available via the compatibility library v4

Base64 decoding of JSONObject in Android 2.1

I need to decode JSONObject in Android 2.1 with Base64.I know that Base64 class supports Android 2.2+,that's why I include the source code in my project.So I need to do something like that :
JSONObject clientHash = new JSONObject();
byte[] tmpSecData = Base64.decode(clientHash.getJSONObject("client_auth_hash"));
Any suggestions how to do that or is it possible?
Lets try it,
Convert the clientHash.getJSONObject("client_auth_hash") in String then byteArray,
then use,
byte temp[];
Base64 b = new Base64();
String jsonString = clientHash.getJSONObject("client_auth_hash").toString();
temp = b.decode(jsonString.getBytes());
then use your temp byte[].
Hope this will help you. If its work then inform me. Thanx.

How to encode a StringBody?

I have a problem when I want to create a StringBody which contains a "ñ" or any extended character. I need to encode it to UTF-8 and I have tried to define the Charset (UTF-8) like this:
new StringBody(i.getValue(), Charset.forName(HTTP.UTF_8));
It doesn't work. Are there any way to encode this String body to UTF-8?
Below is a snippet working with string data type. Try to change the String to StringBody. Hope it works.
String name = "John";
String encodedName = URLEncoder.encode(name, "UTF-8");

Categories

Resources