1

我有一个带有 http GET 请求的 android 客户端,它在服务器上调用一个 php 脚本。我的服务器上有一个 php 脚本,它查询数据库并回显结果行。现在,我如何将 blob 文件和其他字段从响应??我觉得php代码搞砸了..如果你指出我正确的方向会很有帮助..谢谢!

My android code:




HttpClient httpClient = new DefaultHttpClient();

        StringBuilder uriBuilder = new StringBuilder("http://192.168.x.x/file_download.php");


        HttpGet request = new HttpGet(uriBuilder.toString());
        HttpResponse response = httpClient.execute(request);

        int status = response.getStatusLine().getStatusCode();
        Log.i("Http Get",response.getStatusLine().toString());

        // we assume that the response body contains the error message
        if (status != HttpStatus.SC_OK) {
     ByteArrayOutputStream ostream = new ByteArrayOutputStream();
     response.getEntity().writeTo(ostream);
         Log.e("HTTP CLIENT", ostream.toString());
        } else {
    //   InputStream content = response.getEntity().getContent();
         // <consume response>

         String type = response.getEntity().toString();
      content.close(); // this will also close the connection

    }

我的PHP代码:

<?php

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

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("MYDB")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT title, filetype, mode, data, time, application, entity_id 
        FROM   table
        WHERE  id=(select max(id) from file)";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

$rows = array();

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {



    $rows[]=$row;



}

echo $rows;

mysql_free_result($result);

?>
4

1 回答 1

0

我不熟悉 PHP mysql_* 函数,所以我不确定每一行是什么样的。

我认为最简单的方法是使用 PHP 将服务器端的数据格式化为更容易在客户端解析的格式,例如 JSON 或 XML。

我也不知道那部分的 PHP,但这可能是朝着正确方向迈出的一步:Convert MySQL record set to JSON string in PHP

然后您可以使用org.json包类在客户端进行解析。这并不能直接解决您关于分离 blob 的问题,但这样做会使您的 blob 在 JSON 对象上可访问(可能由列名称键入)。

祝你好运。

于 2012-01-10T07:09:02.737 回答