How to get the binary file and other params from HttpResponse in android?

388 views Asked by At

I have an android client with a http GET request invoking a php script on the server.. And a php script on my server which queries the database and echoes the resulting rows.. Now, how do i separate the blob file and other fields from the Response?? I have a feeling that the php code is messed up.. It'll be helpful if you point me in the right direction.. Thanks!

My android code:




HttpClient httpClient = new DefaultHttpClient();

        StringBuilder uriBuilder = new StringBuilder("http://192.168.x.x/file_download.php");


        HttpGet request = new HttpGet(uriBuilder.toString());
        HttpResponse response = httpClient.execute(request);

        int status = response.getStatusLine().getStatusCode();
        Log.i("Http Get",response.getStatusLine().toString());

        // we assume that the response body contains the error message
        if (status != HttpStatus.SC_OK) {
     ByteArrayOutputStream ostream = new ByteArrayOutputStream();
     response.getEntity().writeTo(ostream);
         Log.e("HTTP CLIENT", ostream.toString());
        } else {
    //   InputStream content = response.getEntity().getContent();
         // <consume response>

         String type = response.getEntity().toString();
      content.close(); // this will also close the connection

    }

My php code:

<?php

$conn = mysql_connect("localhost", "root", "root");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("MYDB")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT title, filetype, mode, data, time, application, entity_id 
        FROM   table
        WHERE  id=(select max(id) from file)";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$rows = array();

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {



    $rows[]=$row;



}

echo $rows;

mysql_free_result($result);

?>
1

There are 1 answers

1
dokkaebi On BEST ANSWER

I'm not familiar with the PHP mysql_* functions, so I'm not sure what each row looks like.

I think it would be easiest for you to format the data on the server side, using PHP, into something that will be easier to parse on the client side, such as JSON or XML.

I don't know the PHP for that part either, but this might be a step in the right direction: Convert MySQL record set to JSON string in PHP

Then you could use the org.json package classes to parse on the client side. This doesn't directly address your question about separating the blobs, but doing it this way will make your blobs accessible on the JSON object (probably keyed by the name of the column).

Good luck.