0

what I can use to use instead MultipartEntity ? I have to replace HTTPClinet for AsyncHttpClient and this is code which I have to replace :

File file = new File(filePath);
    if (file.exists()) {
        throw new FileNotFoundException(
                "Plik o podanej nazwie nie istnieje!");
    }
    String name = file.getName();

byte[] data = org.apache.commons.io.FileUtils.readFileToByteArray(file);
HttpClient httpclient = HttpClientUtil.getHttpClient(context);
HttpPost httppost = new HttpPost(Util.getServerUrl(context)
        + "/AddFile");
ByteArrayBody bab = new ByteArrayBody(data, name);
MultipartEntity reqEntity = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("token", new StringBody(String.valueOf(E_Gps.TOKEN)));
reqEntity.addPart("ByteArrayBody", bab);
reqEntity.addPart("filename", new StringBody(name));
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
InputStream responseInputStream = new BufferedInputStream(response
        .getEntity().getContent());
return responseInputStream;
}
Krzysztof Pokrywka
  • 1,356
  • 4
  • 27
  • 50
  • http://stackoverflow.com/questions/27038003/problems-with-a-build-httpbody-httppost-setentity-android/27406857#27406857 – Quick learner May 19 '17 at 05:59

1 Answers1

0

Below code for multipart using this you can upload Image and json data

    public class MultipartRequest {

    private ProgressDialog dialog;
    int resonseCode = 0;
    Context cntx;
    MultipartEntity entity;
    public ArrayList<String> map = new ArrayList<String>();
    public String jsonData;
    private int count = 0;
    private String Url;


    public void SendMultiPartRequest(Context cntx, String Url, String jsonData,
            ArrayList<String> map)

    {
        this.cntx = cntx;
        this.Url = Url;
        this.map = map;
        this.jsonData = jsonData; 


        new ImageUploadTask().execute(count + "", "pk" + count + ".jpg");
    }

    class ImageUploadTask extends AsyncTask<String, Void, String> {

        String sResponse = null;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            CustomDialog.StartProgessBarDialog(cntx);
        }
        @Override
        protected String doInBackground(String... params) {
            try {

                String url = Url;
                int i = Integer.parseInt(params[0]);

                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();

                HttpPost httpPost = new HttpPost(url);
                entity = new MultipartEntity();

                entity.addPart("data", new StringBody(jsonData));


                for (int j = 0; j < map.size(); j++) { 
                    Bitmap bitmap = decodeFile(map.get(j));
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    bitmap.compress(CompressFormat.JPEG, 100, bos);
                    byte[] data = bos.toByteArray();

                    entity.addPart("photos", new ByteArrayBody(data,
                            "image/jpeg", map.get(j)));
                }

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                entity.writeTo(bytes);

                // For adding header param in request

                HashMap<String, String> headers = Constant
                        .getHeaderParameter(cntx);
                for (Map.Entry<String, String> e : headers.entrySet()) {
                    httpPost.setHeader(e.getKey(), e.getValue());
                }

                httpPost.setEntity(entity);

                HttpResponse response = httpClient.execute(httpPost,
                        localContext);

                responseCode = response.getStatusLine().getStatusCode();

                Log.w("Response ", "Status line : "
                        + response.getStatusLine().toString());

                sResponse = EntityUtils.toString(response.getEntity());

                System.out.println("sResponse : " + sResponse);

                JSONObject CheckIsErrorLess = new JSONObject(sResponse);

                String error = "";
                String msg = "";

                if (responseCode == 200) {
                    // Parse your response using gson or any other library 

                    CustomDialog.StopProgessBarDialog(cntx);

                } else {

                // Display warning dialog

                    CustomDialog.StopProgessBarDialog(cntx);
                }

            } catch (Exception e) {
                CustomDialog.StopProgessBarDialog(cntx);

            }
            return sResponse;
        }
        @Override
        protected void onPostExecute(String sResponse) {
            try {

                if (a == 200) {
                    CustomDialog.StopProgessBarDialog(cntx);
                    ((Activity) cntx).finish();
                } else {
                    Constant.OpenErrorDialog(cntx, a);
                }
                if (dialog.isShowing())
                    dialog.dismiss();
                if (sResponse != null) {

                }

            } catch (Exception e) {
                // Log.e(e.getClass().getName(), e.getMessage(), e);

            }

        }
    }
    public Bitmap decodeFile(String filePath) {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 1024;
        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
        return bitmap;
    }
   }
ashish
  • 848
  • 6
  • 16