tags:

views:

145

answers:

4

Here is what I am trying to do.

From my Android App code I do an HTTP POST which is as below

HttpPost httppost = new HttpPost("http://xyz.com/Retrieve.php");

The PHP code is supposed to send back an image and an URL.

$q=mysql_query("SELECT image, url FROM testblob WHERE id = 'id[0]'");
list($data) = mysql_fetch_row($q);
echo $data;

Now in my Android code I do the following and the image is retrieved perfect from the response but how do I retrieve the URL as well.

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

I retrieve the image as below and this part works.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n=is.read(buf))>=0)
    {
    baos.write(buf, 0, n);
    }
    is.close();
    byte[] bytes = baos.toByteArray();
    Bitmap bitmap2 = null;
    bitmap2 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

So I would like to know if both the image and the url is received in the InputStream is object and if yes how do I retrieve the URL out of it?

Thanks for all the help.

+1  A: 

It's a bit more overhead, but it might be easier if you make 2 requests to your server. That way you don't have to worry about separating the bytes.

For the URL request, return a text response.

For the image request, return the bytes (as you're doing now).

m2green
I had that thought in my mind but once again since its a mobile app I am trying to minimize my server trips.
Aakash
This solution definitely works...
Aakash
A: 

You could send an X-header back with the image that contains the URL. Not a standard approach, but this is not a standard problem it seems so a non-standard header may help.

http://www.httpwatch.com/httpgallery/headers/

webbiedave
+1  A: 

You have several options here; here is one. Instead of simply echoing out the bytes for the image from PHP you can encode the image and URL using json_encode().

For example:

$q = $pdo->prepare("SELECT image, url FROM testblob WHERE id = ?");
$q->execute(array($id[0]));
header("Content-Type: application/json");
echo json_encode($q->fetch(PDO::FETCH_NUM));

On the Java side you'll have the read the response from the URL and then parse it appropriately into a JSON object.

I've updated the example to use PDO instead of the old database access functions. You can read up on PHP Data Objects here.

thetaiko
I think this is best option and I will try it out and post how it goes..Thanks.
Aakash
Can you tell me what headers and how do I set it in PHP? That will be very helpful.
Aakash
@Aakash - sorry for the delay. I've updated my answer and also changed the db access to safer PDO functions. You'll have to set up the $pdo variable but its not difficult to do. I've included a link to documentation about that.
thetaiko
Thanks a lot...This works too and this is the more optimized way to do this instead of multiple server trips.
Aakash
A: 

1) Your php code do not use post variable so you retrieve always the same image.

2) After setting the output type you can stream the image back to client;

$q=mysql_query("SELECT image FROM testblob WHERE id = '".$_POST['id']."'");
$row = mysql_fetch_row($q);
header("Content-Type: image/jpeg");
echo $row[0];
Valentino Dell'Aica
Well I assigned the post variables in $id and then I run the query with that.
Aakash
If you need only image data, code like this is the simplest, but if you need to return more datas the best is solution before that encapsulate data in a Json stream.
Valentino Dell'Aica