I do a post request for debugging the application, in response I get bytearray how to display it at least somewhere? If I try to display it in a textview, then I get a crash.

String myURL = "http://s92640jz.bget.ru/register.php"; String params = "login=user123&open_key=rgh24sfs3efs234dir&key_size=4096"; byte[] data = null; InputStream is = null; try { URL url = new URL(myURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); OutputStream os = conn.getOutputStream(); data = params.getBytes("UTF-8"); os.write(data); data = null; conn.connect(); int responseCode= conn.getResponseCode(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); is = conn.getInputStream(); byte[] buffer = new byte[1024]; // Такого вот размера буфер // Далее, например, вот так читаем ответ int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { baos.write(buffer, 0, bytesRead); } data = baos.toByteArray(); } catch (Exception e) { } finally { try { if (is != null) is.close(); } catch (Exception ex) {} } 

    2 answers 2

    The Arrays.toString () method returns a String representation of the array.

     byte[] b1 = new byte[] {97, 98, 99}; String s1 = Arrays.toString(b1); String s2 = new String(b1); Log.v("MYLOG", s1); // -> "[97, 98, 99]" Log.v("MYLOG", s2); // -> "abc"; 
    • It doesn’t work like this: k.myapplication E / AndroidRuntime: FATAL EXCEPTION: main java.lang.IllegalStateException: Could not android.view.View $ 1.onClick (View.java:3599) at android .view.View.performClick (View.java:4204) - Nicola Krivosheya
    • I use this: Log.v (this.getClass (). GetSimpleName (), new String (data)); - Nikola Krivosheya
    • Drop the full error log. - Vladimir VSeos
    • I suspect you have NullPointerException - Vladimir VSeos

    Agree with

    I suspect that you have NullPointerException - Vladimir Seos-Lab

     Log.v("new String() = ", new String(data));