我使用下面的代碼上傳文件到服務器。它在localhost和bluehost中工作得很好。但它不是在(Go Daddy)這樣的主機上工作。有人會解釋爲什麼?Android的http客戶端上傳不工作在某些服務器
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是最好的辦法 –