我想在电子邮件正文中插入html代码。。。在我的代码中,我在后台发送电子邮件意味着按钮的onClick事件,就像每当我在basicnamevauepair中使用Html.formHtml时,它在这一行显示错误一样。。请帮忙
谢谢你
@Override
public void onClick(View arg)
{
String site = "http://2233.comoj.com/mailer.php";
String namer1 = "password";
String to = "[email protected]";
String from = "[email protected]";
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);
}
最佳答案
您需要对toString()
方法调用Html.fromHtml
(因为它返回一个Spanned
),才能将它作为一个值放入NameValuePair
中。
params.add(new BasicNameValuePair("message", Html.fromHtml(message).toString()));