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);