i use AsyncHttpResponseHandler and i have this method:
@Override
public void onSuccess(int statusCode,
org.apache.http.Header[] headers,
byte[] responseBody) {
showProgress(false);
}
But now, how i can get a response in String from byte array ?
One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.
There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.
We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument.
String str = new String(bytes, "UTF-8");
And if you're feeling lazy, you can use the Apache Commons IO library to convert the InputStream to a String directly:
String str = IOUtils.toString(inputStream, "UTF-8");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With