2010-08-09 199 views
0

這裏是我的代碼從服務器位置獲取圖像到一個web服務文件夾"web content"從服務器獲取圖像

// imports removed 

public class WebService { 

    public int writeToFileImage(int a) throws IOException{ 

     File file =new File("sdcard/myImage.jpg"); 
     file.createNewFile(); 

     URL u = new URL("http://172.29.26.40:8080/ExampleService/demo.jpg"); 
     URLConnection uc = u.openConnection(); 
     uc.connect(); 
     InputStream in = uc.getInputStream(); 
     FileOutputStream out; 
     out = new FileOutputStream(file); 
     final int BUF_SIZE = 1 << 8; 
     byte[] buffer = new byte[BUF_SIZE]; 
     int bytesRead = -1; 
     while((bytesRead = in.read(buffer)) > -1) { 
      out.write(buffer, 0, bytesRead); 
     } 
     in.close(); 
     out.close(); 
     return a; 
    } 
} 

但我得到一個例外:

SOAP Response Envelope 
- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
- <soapenv:Body> 
- <soapenv:Fault> 
    <faultcode>soapenv:Server.userException</faultcode> 
    <faultstring>java.net.ConnectException: Connection timed out: connect</faultstring> 
- <detail> 
    <ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">01HW040207</ns1:hostname> 
    </detail> 
    </soapenv:Fault> 
    </soapenv:Body> 
    </soapenv:Envelope> 

可能是什麼錯誤的原因是什麼?

+0

你嘗試使用Web瀏覽器訪問此網址是什麼?也許資源根本不可用?因爲我無法從這裏得到它...... – Sephy 2010-08-09 13:07:13

+0

希望你在描述中清楚你想要做什麼。從你的代碼看起來你想從服務器上將圖像提取到設備的SD卡中。通常你不會公開8080端口。但請確保它可以從您的模擬器訪問。術語SOAP,WebService應該與將圖像下載到設備這一簡單任務無關。 – dipu 2010-08-09 19:41:46

回答

0

我知道這個職位是很老,但 有一個在github上一個偉大的形象下載庫

Android Image Loder

它完美的作品,也是它包括高速緩存(在這裏你也可以關閉),多任務下載等

歡呼 希望這有助於...