【问题标题】:Can't send file via HttpPost multipart to sendgrid API无法通过 HttpPost 多部分将文件发送到 sendgrid API
【发布时间】:2014-05-25 22:23:57
【问题描述】:

我在这个问题上纠结了一段时间。所以我使用 SendGrid 发送电子邮件,除了附加图像文件外一切正常,所以我遵循this的文档

这是我的代码

    private class SendDataInBackGround extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... arg0) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Constants.MAIL_URL);

        try {
            MultipartEntity multipartEntity = new MultipartEntity();
            multipartEntity.addPart("api_user", new StringBody(Constants.MAIL_USERNAME));
            multipartEntity.addPart("api_key", new StringBody(Constants.MAIL_PASSWORD));
            multipartEntity.addPart("to", new StringBody("me@mycompany.com"));
            multipartEntity.addPart("subject", new StringBody("subject"));
            multipartEntity.addPart("text", new StringBody("message"));
            multipartEntity.addPart("from", new StringBody("someone@somewhere.com"));

            File file = new File(Environment.getExternalStorageDirectory()
                    + "/safe.png");
            try {
                file.createNewFile();

            } catch (IOException e1) {
                e1.printStackTrace();
            }
            multipartEntity.addPart("files", new FileBody(file));

            httppost.setEntity(multipartEntity);
            String res = httpclient.execute(httppost, new UploadResponseHandler());
            return res;
        } catch (ClientProtocolException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
         Helper.createToast(getSherlockActivity(), result).show();
    }

    private class UploadResponseHandler implements ResponseHandler<String> {

        @Override
        public String handleResponse(HttpResponse response)
                throws ClientProtocolException, IOException {

            HttpEntity r_entity = response.getEntity();
            String responseString = EntityUtils.toString(r_entity);
            return responseString;
        }

    }
}

我不确定我错过了什么或有更好的解决方案来处理这个问题。

谢谢。

【问题讨论】:

  • 你有读取外部存储权限吗??
  • @IllegalArgument 是的,我有。
  • file.createNewFile 是做什么的?
  • @greenapps 只需创建要上传的模拟文件
  • 什么是mock up?你需要它做什么?

标签: android http-post sendgrid


【解决方案1】:

这是我正在使用并为我工作的代码:

public void testUpload(String path,String url) {
    HttpResponse response;
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost httppost = new HttpPost(url);
        File f = new File(path);
        FileBody b = new FileBody(f);
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("ImageFile", b).build();
        reqEntity.getContentType().toString();
        httppost.setEntity(reqEntity);
        response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            System.out.println("Response content length: "
                    + resEntity.getContentLength());
        }
        Log.d("response", EntityUtils.toString(resEntity));
    } catch (Exception e) {
        // httpclient.close();
        e.printStackTrace();
    }
}

基本相同,我使用这种方法将多个文件发送到我的服务器。希望对您有所帮助,由于您和我的方法相似,因此不需要任何解释,但请随时提问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2012-03-12
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多