1

我有一个具有以下架构的 mysql 数据库

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

1 回答 1

2

您可以将要以 JSON 格式发送的数据编码为

 echo json_encode($response);

并将其作为 Json 发送到您的设备并进行相应的解析。解析参考这里

于 2013-11-12T04:29:32.680 回答