2012-01-10 25 views
1

我有一個Android客戶端與一個HTTP GET請求調用服務器上的一個PHP腳本..和我的服務器上查詢數據庫和回聲結果行的PHP腳本..現在,我如何分開blob文件和其他來自Response的字段?我有一種感覺,PHP代碼搞砸了。如果你指向正確的方向,這將有所幫助。謝謝!如何從android中的HttpResponse獲取二進制文件和其他參數?

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

?> 

回答

0

我不熟悉PHP mysql_ *函數,所以我不知道每一行的樣子。

我認爲使用PHP將服務器端的數據格式化爲更易於在客戶端解析的內容(例如JSON或XML)是最容易的。

我不知道該零件的PHP要麼,但是這可能是一個正確方向邁出的一步:Convert MySQL record set to JSON string in PHP

然後你可以使用org.json包類在客戶端解析。這並不直接解決你關於分離blob的問題,但是這樣做會使你的Blob在JSON對象上可用(可能由列的名稱所控制)。

祝你好運。

+0

嘿,非常感謝..我用json解析服務器端和客戶端,它的功能就像一個魅力..讚賞它! – 2012-01-11 07:35:17

相關問題