How to couple MultipartEntity and List array to send in httppost in android

3k views Asked by At

I am trying to upload a document file to a server using a MultipartEntity. The document is suppose to be uploaded to a particular user account(ie. the users email). So what i did is that I have a List array of namevaluepairs which contains key value pairs of the users email. Giving that the httppost.setEntity() method can only take one argument i am having an issue of coupling both the List array and the MultipartEntity into one object so that I can say httpPost.setEntity(objecy) (ie. the object being the value of both the List of namevaluepair and the multipartentity). How can I achieve this. This is my code that I have thus far.

    @Override
    protected String doInBackground(String... args) {

        // uploadFile(filePath, fileName);
        // get the data from the text fields
        String email = sharedPref.getString("session", "no email");

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("email", email));

        InputStream inputStream;


        try {
            inputStream = new FileInputStream(new File(filePath));
            byte[] data;

            try {


                data = IOUtils.toByteArray(inputStream);

                InputStreamBody inputStreamBody = new InputStreamBody(
                        new ByteArrayInputStream(data), fileName);
                MultipartEntity multipartEntity = new MultipartEntity();
                multipartEntity.addPart("document", inputStreamBody);

                //Here is where i need to figure out how to couple both the List and
                //and multipart before making the httprequest  

                json = jsonParser.makeHttpRequest(url_upload_background,
                        "POST", multipartEntity);  

                Log.d("File to upload",
                        "Multipart is: " + multipartEntity.toString());

                // check log cat for response
                Log.d("Create Response", json.toString());

                try {
                    error = json.getBoolean(TAG_ERROR);
                    message = json.getString(TAG_MESSAGE);

                    Log.d("Error is", "" + error);
                    Log.d("Message is", message);

                    if (error == false) {

                        // Log.d("account type before is ", accountType);

                    } else {

                    }
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        return null;
    }

Here is the code for my json parser where i need to setEntity().

  public JSONObject makeHttpRequest(String url, String method,
        MultipartEntity multipartEntity) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(multipartEntity); //HERE

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }   
1

There are 1 answers

0
Castell James On BEST ANSWER

I finally figured it out. To couple the arraylist of namevaluepairs with the multipartentity I passed the arraylist to the multipartentity as a StringBody object and assigned a key of email to the StringBody.

@Override
    protected String doInBackground(String... args) {

        // uploadFile(filePath, fileName);
        // get the data from the text fields
        String email = sharedPref.getString("session", "no email");

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        Log.d("THE EMAIL IS: ", email);

        params.add(new BasicNameValuePair("email", email));

        InputStream inputStream;


        try {
            inputStream = new FileInputStream(new File(filePath));
            byte[] data;

            try {


                data = IOUtils.toByteArray(inputStream);
                Log.d("File size", ""+ data.toString());
                InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName);
                MultipartEntity multipartEntity = new MultipartEntity();
                multipartEntity.addPart("document", inputStreamBody);
                multipartEntity.addPart("email", new StringBody(email)); //HERE IS THE FIX


                json = jsonParser.makeHttpRequest(url_upload_background,
                        "POST", multipartEntity);

The fix being

multipartEntity.addPart("email", new StringBody(email));