Android http客户端上传无法在某些服务器上运行

时间:2016-06-23 12:25:34

标签: android androidhttpclient

我使用以下代码将文件上传到服务器。它在 localhost bluehost 中运行良好。但它并不像某些托管(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[]检索值时,该值为null。 HttpUrlConnection适用于所有服务器。但我们必须自己写一切。我的意思是为什么要从头开始编写如此多的代码来处理像上传这样的简单函数...是不是有简单回调的库。?

1 个答案:

答案 0 :(得分:0)

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