English 中文(简体)
在Android S D downManager中设置请求方尸体?
原标题:Setting a request body in Android s DownloadManager?

对于我的应用程序的下一步, 我需要添加下载功能。 用户会选择他们想要下载的内容, 并且可以从 1 个文件中选择任何东西到 千个文件, 如果他们愿意选择那么多的话 。

我想使用在下载管理器中建造的Androids 来提供这个下载功能, 但不幸的是,我不明白我怎样才能执行它。

为了让目标服务器授权下载,我需要在请求机构内发送一些 JSON。像这样,如果我手动这样做:

DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(rawData);
output.flush();

rawData 是 JSON 字符串。 请求的正文总是设置为 pOPST

我似乎无法找到任何方法 将这个JSON字符串添加到下载管理器中, 除非我能做到这一点, 服务器将总是拒绝下载 。

我所能想到的唯一另一种解决办法是在我的服务器上写一个PHP脚本, 以取一些 < code> GET 参数, 生成 JSON, 然后再调整请求方向。

有没有人知道一个方法,我可以把我的JSON数据 和下载管理器一起发送?我下载的每个文件 都需要自己独特的JSON字符串。

问题回答

you cannot do that in Android s Download manager, see Download Manager Issue, I had a similar requirement and I ended up Using HttpClient (Xamarin).

样本代码 -

using(var httpClient = new HttpClient()) {
  using(var request = new HttpRequestMessage(new HttpMethod("POST"), URL)) {
   request.Headers.TryAddWithoutValidation("User-Agent", userAgent);
   request.Headers.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
   request.Headers.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.5");
   request.Headers.TryAddWithoutValidation("Connection", "keep-alive");
   request.Headers.TryAddWithoutValidation("Referer",RefererURL);
   request.Content = new StringContent("YOUR_REQUEST_BODY_HERE");
   request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

   var responser = await httpClient.SendAsync(request);
   return responser;
  }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签