我正在使用 Webview 将数据发送到交换邮件服务器。 (http post 不适用于带有大附件的邮件,因此尝试使用此方法)。
请看下面我的代码。
如果我发送的数据根本没有编码,发送就会失败。 如果我按照下面的代码对整个数据进行编码,它仍然会失败。
如果我尝试注释代码,我将数据存储为名称值对并对其进行编码,则会收到邮件但没有附件。那么在这里进行编码的正确方法是什么?附件类型是 ContentBody 。所有其他参数都是字符串。
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,);
entity.addPart("hidid", new StringBody(hidid));
entity.addPart("hidchk", new StringBody(hidchk));
entity.addPart("hidcanary", new StringBody(canary));
entity.addPart("attach", attachment);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
entity.writeTo(bytes);
String fullUrl = baseUrl + "?ae=Dialog&t=Attach&a=Add";
webView.postUrl(fullUrl, EncodingUtils.getBytes(bytes.toString(),"BASE64"));
/* List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("hidid", hidid));
parameters.add(new BasicNameValuePair("hidchk", hidchk));
parameters.add(new BasicNameValuePair("hidcanary", canary));
parameters.add(new BasicNameValuePair("attach", attachment.toString()));
UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(parameters);
String fullUrl = baseUrl + "?ae=Dialog&t=Attach&a=Add";
webView.postUrl(fullUrl, EntityUtils.toByteArray(entity1)); */
我看到他的 Android WebView::postUrl 方法更难编码为“application/x-www-form-urlencoded”。
最佳答案
尝试将附件转换为
public static String encodeToBase64(String string)
{
String encodedString = "";
try
{
byte[] byteData = null;
if(Build.VERSION.SDK_INT >= 8) // Build.VERSION_CODES.FROYO --> 8
{
byteData = android.util.Base64.encode(string.getBytes(),android.util.Base64.DEFAULT);
}
else
{
byteData = Base64Utility.encode(string.getBytes(),Base64Utility.DEFAULT);
}
encodedString = new String(byteData);
}
catch (Exception e)
{
}
return encodedString;
}
转base64
关于机器人 : post data to webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19266502/