android - 如何耦合 MultipartEntity 和 List 数组以在 android 中发送 httppost

标签 android file-upload arraylist http-post multipartentity

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

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

这是我需要 setEntity() 的 json 解析器的代码。

  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();

        }   

最佳答案

我终于明白了。为了将名称值对的数组列表与多方实体相结合,我将数组列表作为 StringBody 对象传递给多方实体,并将电子邮件的键分配给 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));

关于android - 如何耦合 MultipartEntity 和 List 数组以在 android 中发送 httppost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209283/

相关文章:

java - 如何将 ArrayList 中的类的类从一个 Activity 传递到另一个 Activity

java - 如何将泛型类型 ArrayList 设为静态?

java - 对数字 1-10 进行排序,以拼写形式存储在 java 的数组列表中

android - 找不到证书链,如何修复并发布到 Google Play 商店?

php - 快速上传文件到PHP服务器

ruby-on-rails-3 - Ruby on rails 文件上传

javascript - 如何让 jQuery 在上传时限制文件类型?

java - 如何在 Github 包中添加我的 Android 库的依赖项?

android - 如何在 Flutter 中安排通知?

android - 更新失败 java.io.FileNotFoundException