【问题标题】:Android post request in phpphp中的Android发布请求
【发布时间】:2015-06-18 07:54:57
【问题描述】:

每当我发送post request 时,我都会尝试发送php script 发送email。如果我在web browser 中加载我的网站,它确实会发送一封邮件。但是每当我在Android 中发送post request(调用postData 方法)时,什么都没有发生。为什么它不起作用?

public void postData(JSONObject object){
    //not using json object yet since i'm just testing...
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://test.com/email");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<>(2);
        nameValuePairs.add(new BasicNameValuePair("test1", "test1"));
        nameValuePairs.add(new BasicNameValuePair("test2", "test2"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);

        //Just display the response back
        //displayToastMessage(responseBody);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

这里是我喜欢的 php:

public function sendMail()
{
    $test = $_POST["test1"]
    $to = "marco.test@gmail.com";
    $subject = "HTML email";

    $message = "
        <html>
        <head>
        <title>HTML email</title>
        </head>
        <body>
        <p>This email contains HTML Tags!</p>
        <table>
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        </tr>
        <tr>
        <td>$test</td>
        <td>Doe</td>
        </tr>
        </table>
        </body>
        </html>
    ";

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: <webmaster@example.com>' . "\r\n";
    $headers .= 'Cc: myboss@example.com' . "\r\n";

    mail($to,$subject,$message,$headers);
}

【问题讨论】:

  • 你有什么错误
  • 你没有调用你的 sendMail 函数
  • 我用的是Laravel,在路由中声明了sendMail函数,每当加载url时,它会自动调用函数
  • 我没有立即看到错误。但是,HttpUrlConnection 现在是推荐的实现。 HttpClient弃用。你为什么要执行你的帖子两次?你从response.getStatusLine().getStatusCode()得到什么?

标签: php android laravel post


【解决方案1】:

在 PHP 中试试这个代码

$test=$_REQUEST['test1'];

【讨论】:

  • 谢谢!我现在正在使用它,感觉比只使用 GET 更安全
【解决方案2】:
 public function sendMail($data)
    {
        $test = $data["test1"]
        $to = "marco.test@gmail.com";
        $subject = "HTML email";

        $message = "
            <html>
            <head>
            <title>HTML email</title>
            </head>
            <body>
            <p>This email contains HTML Tags!</p>
            <table>
            <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            </tr>
            <tr>
            <td>$test</td>
            <td>Doe</td>
            </tr>
            </table>
            </body>
            </html>
        ";

// Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
        $headers .= 'From: <webmaster@example.com>' . "\r\n";
        $headers .= 'Cc: myboss@example.com' . "\r\n";

        mail($to,$subject,$message,$headers);

    }
sendMail($_REQUEST);

检查这个

【讨论】:

    【解决方案3】:

    我想我找到了问题所在,并且认为我也可以帮助其他人。我正在使用 Laravel 调用该函数,但显然 Laravel 不喜欢那样。我下载了一个名为 Postman 的 Chrome 工具并手动发送了一个 Post 请求,这引发了一个错误,可能是为了防止跨站点请求伪造或其他类型的黑客攻击。现在我把它做成了一个单独的文件,它现在可以工作了!

    【讨论】:

    • 太棒了!确保在可能的情况下接受自己的答案。以便其他人可以更轻松地找到您的解决方案。
    • 我会的 :) 我要等两天它说... :/ 但感谢@Knossos 的支持!
    猜你喜欢
    • 2011-01-19
    • 1970-01-01
    • 2012-07-06
    • 2015-12-06
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多