【问题标题】:The constructor BasicNameValuePair(String, Spanned) in undefined while using Html.fromHtml(message)使用 Html.fromHtml(message) 时未定义的构造函数 BasicNameValuePair(String, Spanned)
【发布时间】:2015-02-20 09:41:45
【问题描述】:

我想在电子邮件正文中插入 html 代码...在我的代码中,我在后台发送电子邮件意味着按钮的 onClick 事件,因为每当我在 BasicNameValuePair 中使用 Html.formHtml 时,它都会在此行显示错误...请帮助 谢谢你

@Override
public void onClick(View arg)
{
    String site = "http://2233.comoj.com/mailer.php";
    String namer1 = "password";
    String to = "abc@gmail.com";
    String from = "xyz@gmail.com";
    String subject1 = "checking mail";
    String message = "<html><head><body><h1>Hello World</h1></body></head></html>";
    String content = "";

    try
    {
        /* Sends data through a HTTP POST request */
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(site);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", namer1));
        params.add(new BasicNameValuePair("to", to));
        params.add(new BasicNameValuePair("from", from));
        params.add(new BasicNameValuePair("subject", subject1));
        params.add(new BasicNameValuePair("message", Html.fromHtml(message))); //error in this line 
        httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        /* Reads the server response */
        HttpResponse response = httpClient.execute(httpPost);
        InputStream in = response.getEntity().getContent();
        StringBuffer sb = new StringBuffer();
        int chr;
        while ((chr = in.read()) != -1)
        {
            sb.append((char) chr);
        }
        content = sb.toString();
        in.close();
        /* If there is a response, display it */
        if (!content.equals(""))
        {
            Log.i("HTTP Response", content);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    Intent intent = new Intent(context, Invite2.class);
    startActivity(intent);
}

【问题讨论】:

  • 请在哪一行发logcat
  • 在这一行 params.add(new BasicNameValuePair("message", Html.fromHtml(message)));当我保存我的代码错误显示“未定义的构造函数 BasicNameValuePair(String, Spanned)”

标签: android html email


【解决方案1】:

您需要在Html.fromHtml 方法上调用toString()(因为它返回Spanned)才能将其作为值放入NameValuePair

params.add(new BasicNameValuePair("message", Html.fromHtml(message).toString()));

【讨论】:

    【解决方案2】:

    根据Html参考 方法fromHtml 是返回一个Spanned

    我认为你只是使用发送String

    params.add(new BasicNameValuePair("message", message));
    

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多