0

I've a server and a client. My server sends a HttpResponse in a text format. I'm able to receive the text at client. I need to convert back the text to HttpResponse object. Are there any opensource libraries or in-built java/Android mechanisms to do this?

All I've got is converting HttpResponse to text.

Kishore
  • 952
  • 2
  • 11
  • 31
  • How do you receive the byteArray? – Tim Nov 04 '14 at 08:33
  • using recv system call. – Kishore Nov 04 '14 at 08:38
  • `My server sends a HttpResponse in a byteArray`. A http server is a http server and as such will send just text, pictures or docs. It is just always a stream of bytes. So i wonder what you mean by a response as byte array. – greenapps Nov 04 '14 at 09:50
  • @greenapps Sorry my mistake. I've edited the question. – Kishore Nov 04 '14 at 10:12
  • `I need to convert back the text to HttpResponse object` ??? What do you mean? And why would you? For what? It will never have been a response object before. Why do you think you need something? What do you want to do with the received text? – greenapps Nov 04 '14 at 10:19
  • What do you want to do with the received text? I'll read the header, data and other information from the text I receive. As it is a simple text now I could not parse it efficiently, Thats why I need a HttpParser. I guess this answers all the questions. – Kishore Nov 04 '14 at 10:36
  • If it is a html text you can use an xml parser like JSOUP. – greenapps Nov 04 '14 at 12:27
  • Its not an HTML, Its a kind of "HTTP/1.1 200 OK Content-Length: 100" – Kishore Nov 04 '14 at 13:23

3 Answers3

0

Try this way

ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.getEntity().writeTo(baos);
byte[] bytes = baos.getBytes();

Then you could add the content to another HttpResponse object as follows:

HttpResponse response = httpClient.execute(new HttpGet(URL));
response.setEntity(new ByteArrayEntity(bytes));
Maveňツ
  • 1
  • 12
  • 50
  • 89
0

Just do this using Apache Commons IO: http://commons.apache.org/proper/commons-io/

private byte[] receiveByteArrayData(HttpURLConnection httpURLConnection)
    throws IOException
{
    return IOUtils.toByteArray(httpURLConnection.getInputStream());
}

Sending byte[] data:

    URL url = new URL("http://your.url.here:yourPortNumber/something");
    HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
    httpUrlConnection.setDoInput(true);
    httpUrlConnection.setDoOutput(true);
    httpUrlConnection.setRequestProperty("Content-Type", "application/octet-stream");
    httpUrlConnection.setRequestMethod("POST");
    httpUrlConnection.setFixedLengthStreamingMode(bytes.length);

    httpUrlConnection.connect();

    httpUrlConnection.getOutputStream().write(bytes);
    httpUrlConnection.getOutputStream().flush();
    if (httpsUrlConnection.getResponseCode() == 200) //OK
    {
        System.out.println("successfully uploaded data");
    }
    httpUrlConnection.disconnect();
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

I got exactly what I want from parse http response bytes in java

Thanks everyone for responses :)

Community
  • 1
  • 1
Kishore
  • 952
  • 2
  • 11
  • 31