安卓应用赢得';t将post数据发送到php文件


Android app won't send post data to php file

我的Android应用程序中有一个函数,它将数据发送到一个PHP文件,该文件将数据插入数据库。我的问题是post值从未真正发送到PHP文件。

我的功能如下:

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();
    }
}

我的php文件只是检查变量是否已设置:

<?php
if (isset($_POST["firstName"]) && isset($_POST["lastName"])) {
    // insert into the database
} else {
    // send error message.
}
?>

我总是从PHP文件的其他部分得到错误消息。有人知道为什么会发生这种情况或如何解决吗?

您的问题可能存在于跨域策略文件中。服务器必须有一个跨域.xml文件,允许服务器之外的用户向其发送数据。

谷歌crossdomain.xml获取更多信息。