从android中的php服务器下载pdf
我创建了一个php代码,用于从服务器下载pdf,效果很好。现在我想在Android id应用程序中使用这个php文件,并从Android服务器下载pdf。我该怎么做?我的php代码:从android中的php服务器下载pdf,android,file,download,Android,File,Download,我创建了一个php代码,用于从服务器下载pdf,效果很好。现在我想在Android id应用程序中使用这个php文件,并从Android服务器下载pdf。我该怎么做?我的php代码: <?php $id = $_GET['bookid']; $sql = "SELECT file,BookName FROM books WHERE id=" . prepareSQL($id) . " "; $query = mysql_query($sql) or die('Error, quer
<?php
$id = $_GET['bookid'];
$sql = "SELECT file,BookName FROM books WHERE id=" . prepareSQL($id) . " ";
$query = mysql_query($sql) or die('Error, query failed');
$content = mysql_fetch_array($query);
header("Content-length: $size");
header("Content-type: $type");
$name=$content['BookName'];
header("Content-Disposition: attachment; filename=$name");
echo $content['file'];
?>
您可以使用这个
public static void downloadAndOpenPDF(final Context context, final String pdfUrl) {
// Get filename
final String filename = pdfUrl.substring( pdfUrl.lastIndexOf( "/" ) + 1 );
// The place where the downloaded PDF file will be put
final File tempFile = new File( context.getExternalFilesDir( Environment.DIRECTORY_DOWNLOADS ), filename );
if ( tempFile.exists() ) {
// If we have downloaded the file before, just go ahead and show it.
openPDF( context, Uri.fromFile( tempFile ) );
return;
}
// Show progress dialog while downloading
cm=new CustomLoaderDialog(context);
cm.show(true);
//final ProgressDialog progress = ProgressDialog.show( context, "AIH" , "Please Wait", true );
// Create the download request
DownloadManager.Request r = new DownloadManager.Request(Uri.parse(pdfUrl));
r.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,filename);
final DownloadManager dm = (DownloadManager) context.getSystemService( Context.DOWNLOAD_SERVICE );
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ( !cm.isShowing() ) {
return;
}
context.unregisterReceiver( this );
cm.hide();
long downloadId = intent.getLongExtra( DownloadManager.EXTRA_DOWNLOAD_ID, -1 );
Cursor c = dm.query( new DownloadManager.Query().setFilterById( downloadId ) );
if ( c.moveToFirst() ) {
int status = c.getInt( c.getColumnIndex( DownloadManager.COLUMN_STATUS ) );
if ( status == DownloadManager.STATUS_SUCCESSFUL ) {
openPDF( context, Uri.fromFile( tempFile ) );
}
}
c.close();
}
};
context.registerReceiver( onComplete, new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE ) );
// Enqueue the request
dm.enqueue( r );
}