2014-02-07 186 views
0

我嘗試從php服務器發送文件到Andorid客戶端。這是我的Java代碼:將文件從php服務器發送到Android客戶端

InputStream in = null; 
try { 
HttpClient httpclient = new DefaultHttpClient(); 
HttpResponse response = httpclient.execute(new HttpGet(URL)); 

in = response.getEntity().getContent(); 
} 
    catch (Exception e) { 
Log.e("[GET REQUEST]", "Network exception"); 
    } 


    String fileName = "form.xml"; 
    File destinationfile = new File(Environment.getExternalStorageDirectory() + "/ServiceHelper/" + fileName); 


BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destinationfile)); 
byte byt[] = new byte[1024]; 
int i; 

for (long l = 0L; (i = in.read(byt)) != -1; l += i) { 
    buffer.write(byt, 0, i); 
} 

我不知道應該怎麼看起來像在PHP服務器的一部分,發送和保存文件,例如,德克薩斯州,或XML。

我將您的任何建議

回答

0

簡化的例子在正確的地方等

String fileName = "form.xml"; 
URL url = new URL("http://example.com/form.php"); 
File destinationfile = new File(Environment.getExternalStorageDirectory() + "/ServiceHelper/" + fileName); 
URLConnection connection = url.openConnection(); 
input = connection.getInputStream(); 
byte[] buffer = new byte[4096]; 
int n = - 1; 
OutputStream output = new FileOutputStream(file); 
while ((n = input.read(buffer)) != -1) 
{ 
    output.write(buffer, 0, n); 
} 
output.close(); 

//Refresh file location MediaScannerConnection 
if (destinationfile != null) { 
    MediaScannerConnection.scanFile(context, new String[] { destinationfile.toString() }, null, null); 
} 

在PHP從w3schools加試捕非常感謝:

form.php

<?php 
header("Content-type: text/xml"); 
echo "<?xml version='1.0' encoding='UTF-8'?>"; 
echo "<note>"; 
echo "<from>Jani</from>"; 
echo "<to>Tove</to>"; 
echo "<message>Remember me this weekend</message>"; 
echo "</note>"; 
?> 
相關問題