2013-03-03 94 views
2

在android中,我有這個URL,我必須做POST請求 返回的輸入流的所述請求返回一個可下載的對象... 我該如何使用Android Downloadmanager本身或自定義創建一個,爲我處理下載過程?Android HTTP POST下載管理器

+0

什麼是你的目標API? – 2013-03-03 14:10:47

+0

最低支持2.3.6 在4.1平板電腦上開發,但它主要用於2.3.6 – jaggy 2013-03-03 14:14:06

+0

看看這個http://stackoverflow.com/questions/2323617/android-httppost-how-to-get-the-result幫助。 – 2013-03-03 14:20:24

回答

0

的Android的下載管理器是相當簡單:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
Request request = new Request(Uri.parse("http://www.theeUrl.fake/image.png")); 
enqueue = dm.enqueue(request); 

,你可以輕鬆地添加參數爲GET請求。我不知道你是否可以從它發出POST請求。

編輯:做一個POST請求您可以使用HttpClient的是這樣的:

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 
HttpResponse response = null; 
Dictionary<String, String> postFields = new Dictionary<String, String>(); 

// Set the post fields 
postFields.add("username","toto"))); 
postFields.add("password", "thePassword45155"))); 
post.setEntity(new UrlEncodedFormEntity(postFields, HTTP.UTF_8)); 

// Execute the POST request 
response = client.execute(post); 

,你應該能夠從獲得的HttpResponse的InputStream要下載的文件。 但您必須自己管理顯示/通知/取消...。

+0

這就是我已經知道的,這是關鍵,我需要一個POST請求,而不是一個GET請求 – jaggy 2013-03-03 14:13:20

+0

得到了這一點;)恐怕你不能使用下載管理器,然後...我編輯我的添加另一個解決方案 – Guian 2013-03-03 14:18:29

+0

我已經做了所有的東西:(我發現的解決方案,嗯,我基本上不得不自己創建我自己的通知和更新它,而不是自動 – jaggy 2013-03-03 15:22:16