机器人 : post data to webview

标签 android post encoding webview

我正在使用 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/

相关文章:

php - 发布 HTML 表单后重定向

post - JQuery Mobile - 发布表单并获得响应

php - 用于验证 linux 文件名的正则表达式(多编码)

java - 在 idea intellij 中更改编码不起作用

javascript - `$(' #form ').serialize()` 弄乱 UTF-8 字符

android - 通过 webview 从 Activity 导航到互联网后失去控制

java - Android:检索网站 html - 不起作用

php - Symfony2 app.php 接收 POST 请求作为 GET app_dev.php 工作正常

安卓模拟器 : Unfortunately Launcher has stopped

java - 如何将循环中的值放入数组列表中?