简体   繁体   English

Android应用程序不会将发布数据发送到php文件

[英]Android app won't send post data to php file

I have a function in my Android app that sends data to a PHP file which inserts it into a database. 我的Android应用程序中有一个函数,可将数据发送到PHP文件,然后将其插入数据库。 My problem is that the post values never actually get sent to the PHP file. 我的问题是,发布值实际上从未发送到PHP文件。

My function looks like this: 我的函数如下所示:

public void postData(){

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(myURL);
    try {
        List<NameValuePair> param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("firstName", "John"));
        param.add(new BasicNameValuePair("lastName", "Doe"));

        post.setEntity(new UrlEncodedFormEntity(param));
        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

          // show response in logcat
          String line = "";
          while ((line = rd.readLine()) != null) {
            Log.i("postData", line);
           }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

My php file is simply a check if the the variables were set: 我的php文件只是检查变量是否已设置:

<?php

if (isset($_POST["firstName"]) && isset($_POST["lastName"])) {

    // insert into the database

} else {

    // send error message.

}
?>

I always just get the error message from the else part of the PHP file. 我总是总是从PHP文件的else部分得到错误消息。 Does anybody know why this is happening or how to fix this? 有人知道为什么会这样或如何解决吗?

Your problem could reside in a crossdomain policy file. 您的问题可能存在于跨域策略文件中。 The server must have a crossdomain.xml file that allows users outside the server to send data to it. 服务器必须具有crossdomain.xml文件,该文件允许服务器外部的用户向其发送数据。

Google crossdomain.xml for more information. 有关更多信息,请参见Google crossdomain.xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM