2012-01-10 15 views
1

私はサーバー上でPHPスクリプトを呼び出すhttp GETリクエストを持つアンドロイドクライアントを持っています。そして、データベースを照会し結果の行をエコーする私のサーバー上のPHPスクリプト..今、どのように私はblobファイルとレスポンスのフィールド私は、PHPコードが混乱していると感じている..あなたが正しい方向に私を向けると役立つでしょう..ありがとう!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解析を使用し、それは魅力のように動作します..それを感謝します! –

関連する問題