我想從Android設備發送我的文件與web api與參數的名稱,塊,文件數據。我有這樣的代碼:它不會返回任何錯誤,我不知道我做錯了什麼。請幫助我花這麼多時間,但它仍然不起作用。如何使用http客戶端上傳Web Api中的圖片?
FileBody localFileBody = new FileBody(new File("/storage/sdcard0/Pictures/Images/angel_productshot.jpg"),
"image/jpg");
MultipartEntity localMultipartEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpPost httpPost = new HttpPost(
"http://phdsafar4.myserver.com.ph:1217/api/fileupload");
HttpClient httpclient = new DefaultHttpClient(httpParameters);
httpPost.addHeader("Authorization","Basic " + Base64.encodeToString(
(username + ":" + password).getBytes(), Base64.NO_WRAP));
httpPost.setHeader("Content-Type", "multipart/form-data");
httpPost.setHeader("Accept", "application/image");
localMultipartEntity.addPart("name", new StringBody(filename));
localMultipartEntity.addPart("chunk", new StringBody("1"));
localMultipartEntity.addPart("file data", localFileBody);
httpPost.setEntity(localMultipartEntity);
HttpResponse localHttpResponse = httpclient.execute(httpPost);
Log.i("response upload", localHttpResponse.toString());
Log.i("Multipart Entity", localMultipartEntity.toString());
檢查http://stackoverflow.com/questions/4623507/post-values-and-upload-image-to-php-server-in -android/4633474#4633474上傳圖片到php web服務的答案。 –
請告訴我我的代碼爲什麼沒有輸出。 – NewDroidDev