1

I have a mysql database with the following schema

(int id, int sys_id, BLOB data)

There can be multiple data for the same sys_id, i.e. (1,1, blobdata1), (2,1, blobdata2) etc. are valid entries. the blob data is a compressed audio. When all the rows for a particular sys_id are combined a valid compressed audio data is produced.

I want to send this blob data to my android device. I have tried the following php code to send the data but it is not received as expected at the client side.

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

global $blobId;
$blobId = $_GET['id'];
$result = mysql_query("SELECT data FROM table WHERE sys_id=$blobId");
if( mysql_num_rows($result) == 0 )
die("No rows returned");

while($row = mysql_fetch_array($result) ) {
     // is this correct way of concatenating binary data
     $temp .= $row['data'];
}

// PROBLEM: what should be sent
echo $temp;

I don't mind if all the rows can be received at the client end and can be concatenated or operated upon locally there.

At the client side, I do the following:

// connect to the server
public void connect(URL url)
{

HttpURLConnection urlConnection = null;
    try {           
        urlConnection = (HttpURLConnection)url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        result = readStream(in);
            //problem, how should I now parse the resultant string
            decompress(result.getBytes()); // result.getBytes() returns null currently

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// for reading the incoming stream
public static String readStream(InputStream in) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader r = new BufferedReader(new InputStreamReader(in),1000);
    for (String line = r.readLine(); line != null; line =r.readLine()){
        sb.append(line);
    }
    in.close();
    return sb.toString();
} 

// definition for decompression utility
 public short[] decompress(byte[] codBytes);
2
  • You can send the audio as binary data as a string. And you can covert this binary data to an audio file from your android device. Commented Jun 5, 2012 at 6:40
  • @fargath The data is in compressed form. I am decompressing it at the client end. I tried echoing the generated value($temp) and then tried result.getBytes() at the client end (see the code above). But it doesn't help. Commented Jun 5, 2012 at 6:43

1 Answer 1

2

you can encode the data you want to send in JSON format as

 echo json_encode($response);

and send it as Json to your device and parse accordingly. for parsing refer Here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.