Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can get Response in String from Byte

Tags:

android

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 ?

like image 623
FelasDroid Avatar asked Nov 16 '13 21:11

FelasDroid


People also ask

Can you convert byte into string?

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.

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

How do you write bytes on a string?

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.


1 Answers

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");
like image 105
Kashif Nazar Avatar answered Sep 22 '22 20:09

Kashif Nazar