php - 将多文本从 Android 发送到 API

标签 php android api android-edittext

我在 android 中创建了一个“联系我们表单”,它使用 PHP Api 向我发送电子邮件。例如,在我键入的 EditText 消息中:

This is line 1
This is line 2

当我收到电子邮件时,它会显示:

This is line 1\nThis is line 2\n

发送邮件的代码:

         String myURL = myDomain+myPHPDir+"sendMessageFromContactUs.php?pMyUsername="+myUsername
             +"&pName="+strName
             +"&pEmail="+strEmail
             +"&pMessage="+strMessage
             ;
     String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n", "%0A");
     task.execute(new String[] {noSpace});

最佳答案

我是通过以下方式解决的。

在 PHP 上:

  • 首先"\n" or "\r\n"对我不起作用,它打印在同一行上。
  • 其次"<br>"工作,它在新行上打印字符。

然后我从下面的来源阅读了一些关于“URL 转义字符的完整指南”的内容:

https://www.werockyourweb.com/url-escape-characters/

所以:

for <br> this < is %3C AND this > is %3E THEREFORE <br> becomes %3Cbr%3E

在安卓上:

我改变了(没用):

String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%0A");

致(工作):

String noSpace = myURL.replaceAll(" ","%20").replaceAll("\n","%3Cbr%3E");

现在我可以在 EditText 中编写段落,Outlook 会正确显示它们。

干杯!

更新:(只是添加)

这些字符的解释方式(我后来了解到,也是分享此更新的原因)是因为发送邮件的 php 脚本中使用的 header :(设置 $header 时使用的字符text/html 内容类型(如下面的代码所示)将使用 HTML(不再是 PHP)转义字符)

$headers = "Content-type: text/html; charset=\"UTF-8\";

关于php - 将多文本从 Android 发送到 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388104/

相关文章:

php - 在 JavaScript 中使用 PHP

php - xampp 中如何解决 “Warning: World-writable config file ‘/opt/lampp/etc/my.cnf’ 被忽略”

Android Studio断点交叉解释

java - 在内部存储的文件夹内创建文件

android - 通过js在WebView中获取位置

php - Facebook登录卡住空白页

php - 将作业添加到 beanstalkd

python - 如何使用 Uber API 的个人访问 token ? (获取 'No authentication provided.' )

web-services - 如何使用 Youtube API 在单个用户的视频中搜索?

api - youtube API:获取用户上传的最受欢迎视频