2012-01-10 1 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

I *이 기능을 PHP의 mysql_로에 익숙하지 않은, 그래서 나는 각 행이 어떻게 생겼는지 모르겠어요.

PHP를 사용하여 서버 측에서 데이터를 형식화하는 것이 JSON 또는 XML과 같이 클라이언트 측에서 더 쉽게 구문 분석 될 수 있다고 생각합니다.

는 나도 그 부분에 대한 PHP를 모르는, 그러나 이것은 올바른 방향으로 단계가 될 수 있습니다

Convert MySQL record set to JSON string in PHP는 그런 다음 클라이언트 측에서 구문 분석 org.json 패키지의 클래스를 사용할 수 있습니다. 이것은 blob을 분리하는 것에 대한 여러분의 질문을 직접적으로 다루지는 않지만이 방법을 사용하면 blob에 JSON 객체에서 액세스 할 수 있습니다 (아마도 열의 이름을 키로 입력).

행운을 빈다.

+0

안녕하세요, 고맙습니다. 저는 json 파싱을 서버와 클라이언트 측에서 모두 사용했으며 매력과 같이 작동합니다. 감사합니다 !! –

관련 문제