【问题标题】:Android http client upload not working in some serverAndroid http客户端上传在某些服务器上不起作用
【发布时间】:2016-06-23 12:25:33
【问题描述】:

我正在使用以下代码将文件上传到服务器。它在 localhostbluehost 中运行良好。但它不适用于某些托管(例如(Go Daddy))。谁能解释一下原因???

   data = IOUtils.toByteArray(inputStream);
   HttpClient httpClient = new DefaultHttpClient();
   HttpPost httpPost = new HttpPost(upload_url);
   InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName);
   CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity();
   multipartEntity.setUploadProgressListener(this);
   multipartEntity.addPart("pdf", inputStreamBody);
   multipartEntity.addPart("title", new StringBody(title));
   multipartEntity.addPart("subject", new StringBody(subject));
   multipartEntity.addPart("college", new StringBody(college));
   multipartEntity.addPart("stream", new StringBody(stream));
   multipartEntity.addPart("branch", new StringBody(branch));
   multipartEntity.addPart("userID", new  StringBody(String.valueOf(userid)));
   httpPost.setEntity(multipartEntity);
   HttpResponse httpResponse = httpClient.execute(httpPost);

我进行了很多测试。当我通过服务器中的$_POST[] 检索值时,该值为空。 HttpUrlConnection 适用于所有服务器。但是我们必须自己编写所有内容。我的意思是为什么要从头开始为像上传这样的简单功能编写这么多代码...没有带有简单回调的库吗?

【问题讨论】:

  • 可能是,由于 HttpClient 现在已弃用!所以你应该尝试使用 HttpURLConnection
  • HttpURLConnection 有效,但我们必须写很多废话。如果您使用过并且运行良好,请向我推荐一些图书馆
  • multipartentity 也被弃用了...httpurlconnection 是最好的方法

标签: android androidhttpclient


【解决方案1】:

HttpClient、HttpPost.. 在 API 22 中已弃用,因此您应该尝试改用 HttpURLConnection。

【讨论】:

  • 我不想从头开始重写......我的意思是我必须在httpurlconnection中写很多废话。如果你用过的话,给我推荐一些图书馆...
  • 您有不同的选项来管理 Android 中的网络。按照这篇文章获取替代库:stackoverflow.com/questions/6341400/…
猜你喜欢
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 2021-10-18
相关资源
最近更新 更多