2016-06-23 31 views
0

我使用下面的代碼上傳文件到服務器。它在localhostbluehost中工作得很好。但它不是在(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適用於所有服務器。但我們必須自己寫一切。我的意思是爲什麼要從頭開始編寫這麼多的代碼來實現上傳等簡單功能......是否沒有使用簡單回調的庫?

+0

可能是因爲HttpClient現在已經棄用!所以你應該嘗試使用HttpURLConnection –

+0

HttpURLConnection的作品,但我們必須寫很多胡扯。建議我一些圖書館,如果你用過一個,效果不錯 –

+0

multipartentity也被棄用... httpurlconnection是最好的辦法 –

回答

0

HttpClient,HttpPost ..在API 22中被棄用,所以你應該嘗試使用HttpURLConnection來代替。

+0

我不想從頭開始重寫東西...我的意思是我必須在httpurlconnection中寫很多胡扯。建議我一些庫,如果你使用過一個庫... –

+0

你有不同的選項來管理Android中的網絡。 按照這篇文章替代庫:http://stackoverflow.com/questions/6341400/alternative-http-client-library-for-android – alway5dotcom

相關問題