2012-06-05 215 views
1

我有一个MySQL数据库与下面的模式从MySQL数据库获取BLOB到Android

(int id, int sys_id, BLOB data) 

可以有多个数据相同sys_id,即(1,1,blobdata1),(2,1, blobdata2)等是有效的条目。 blob数据是压缩音频。当特定sys_id的所有行被组合时,产生有效的压缩音频数据。

我想发送这个blob数据到我的android设备。我已经尝试了下面的PHP代码来发送数据,但它没有按预期在客户端收到。

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

我不介意是否所有的行都可以在客户端接收,并可以在本地连接或操作。

在客户端,我做到以下几点:

// 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); 
+0

您可以将音频作为二进制数据以字符串形式发送。你可以将这个二进制数据转换成Android设备上的音频文件。 – fargath

+0

@fargath数据以压缩格式显示。我在客户端解压缩它。我尝试回显生成的值($ temp),然后在客户端尝试result.getBytes()(请参阅上面的代码)。但它没有帮助。 – krammer

回答

2

你可以编码的数据要在JSON格式发送作为

echo json_encode($response); 

并将其作为JSON来您的设备和相应地解析。 解析请参考Here