【发布时间】: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