如何结合MultipartEntity和List数组在android中的httppost中发送

时间:2014-11-30 02:11:01

标签: android file-upload arraylist http-post multipartentity

我正在尝试使用MultipartEntity将文档文件上传到服务器。假设该文档被上载到特定用户帐户(即用户电子邮件)。所以我做的是我有一个namevaluepairs的List数组,其中包含用户电子邮件的键值对。假设httppost.setEntity()方法只能接受一个参数我有一个将List数组和MultipartEntity耦合到一个对象的问题,这样我就可以说httpPost.setEntity(objecy)(即对象是值) namevaluepair和multipartentity列表。我怎样才能做到这一点。这是我到目前为止的代码。

    @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;
    }

这是我的json解析器的代码,我需要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 个答案:

答案 0 :(得分:1)

我终于明白了。为了将namevaluepairs的arraylist与multipartentity结合,我将arraylist作为StringBody对象传递给multipartentity,并将一个电子邮件密钥分配给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);

修复

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