【问题标题】:How to couple MultipartEntity and List array to send in httppost in android如何耦合 MultipartEntity 和 List 数组以在 android 中发送 httppost
【发布时间】:2014-11-30 02:11:00
【问题描述】:

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

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

        }   

【问题讨论】:

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


    【解决方案1】:

    我终于明白了。为了将 namevaluepairs 的 arraylist 与 multipartentity 耦合,我将 arraylist 作为 StringBody 对象传递给 multipartentity,并将 email 的键分配给 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多