0

멀티 파트를 사용하여 발리를 통해 이미지를 업로드하고 이미지 업로드 중에 진행률 대화 상자를 표시하려고합니다. 이미지가 업로드 될 때멀티 진행 이미지를 발리 가로 진행 막대로 보냅니다.

나는 this code for show progress dialog 사용이고 또한 check this code for this.

및 업로드 코드 아래 사용합니다.

public void doFileUpload(ArrayList<MyUploadImage> images){ 
     try { 
      //MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); 
      //entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 

      JSONObject jo = new JSONObject(); 
      jo.put("NoOfImages", images.size()); 

      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(link); 

      CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener() 
      { 
       @Override 
       public void transferred(long num) 
       { 
        pd.setProgress((int) ((num/(float) totalSize) * 100)); 
       } 
      }); 

      //MultipartEntity reqEntity = new MultipartEntity(); 
      int size = images.size(); 
      for(int i = 0; i < size; i++){ 
       FileBody bin1 = new FileBody(images.get(i).getImageFile()); 

       multipartContent.addPart(("uploaded_file"+i), bin1); 
      } 

      multipartContent.addPart("girish", new StringBody(jo.toString())); 
      totalSize = multipartContent.getContentLength(); 

      post.setEntity(multipartContent); 
      HttpResponse response = client.execute(post); 

      HttpEntity resEntity = response.getEntity(); 

      final String response_str = EntityUtils.toString(resEntity); 

      Log.e("Response", response_str); 

      pd.dismiss(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
      pd.dismiss(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      pd.dismiss(); 
     } 

    } 

감사

답변

0

발리 나던 여러 부분에 대한 지원을 제공합니다. 그러나 당신은 여전히 ​​발리의 프레임 워크를 사용할 수 있으며 SSL 연결에 사용했던 것처럼 HttpStack을 자신의 구현에 제공 할 수 있습니다.

public class MultiPartRequest extends JsonRequest<JSONObject> { 

    /* To hold the parameter name and the File to upload */ 
    private Map<String,File> fileUploads = new HashMap<String,File>(); 

    /* To hold the parameter name and the string content to upload */ 
    private Map<String,String> stringUploads = new HashMap<String,String>(); 

    public void addFileUpload(String param,File file) { 
     fileUploads.put(param,file); 
    } 

    public void addStringUpload(String param,String content) { 
     stringUploads.put(param,content); 
    } 

    public Map<String,File> getFileUploads() { 
     return fileUploads; 
    } 

    public Map<String,String> getStringUploads() { 
     return stringUploads; 
    } 
} 

그리고 HttpStack 구현에서 MultiPartEntity를 만들고 HttpRequest로 설정하십시오. 자세한 내용은 SslHttpStack createHttpRequest 메서드를 참조하십시오. https://github.com/smanikandan14/Volley-demo

:

private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest, Request<?> request) throws AuthFailureError { 

    // Return if Request is not MultiPartRequest 
    if(request instanceof MultiPartRequest == false) { 
     return; 
    } 

    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    //Iterate the fileUploads 
    Map<String,File> fileUpload = ((MultiPartRequest)request).getFileUploads(); 

    for (Map.Entry<String, File> entry : fileUpload.entrySet()) { 
     multipartEntity.addPart(((String)entry.getKey()), new FileBody((File)entry.getValue())); 
    } 

    //Iterate the stringUploads 
    Map<String,String> stringUpload = ((MultiPartRequest)request).getStringUploads(); 

    for (Map.Entry<String, String> entry : stringUpload.entrySet()) { 
     try { 
      multipartEntity.addPart(((String)entry.getKey()), new StringBody((String)entry.getValue())); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    httpRequest.setEntity(multipartEntity); 
} 

여기에이 soluction를 참조하십시오