【问题标题】:android with php web service带有 php 网络服务的 android
【发布时间】:2014-11-11 04:23:05
【问题描述】:

我正在为 web 服务开发一个带有 php 的 android 应用程序。在我的应用程序中,我在 php 文件中插入、更新、删除和搜索查询并保存到 www root,然后我将 php 文件调用到 android 应用程序中。搜索功能运行良好,但删除功能不起作用。我已经附上了php文件,android代码和错误。谁能帮帮我???

 **filename** (deletedetail.php)

<?php
include "config.php";

$id = $_POST['id'];

$sql1="delete from detail where id ='$id'";

$result=mysql_query($sql1);

 if($result)
{
  echo"OK";
}
  else
{
  echo "Error";

}

?>






**android CODE**

public String deleteDetail(int id) { 
        try {

            httpclient = new DefaultHttpClient(); 
            httppost = new HttpPost(
                    "http://10.0.2.2/dreamhotel/deletedetail.php"); // change this to your URL.....
            nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("id",Integer.toString(id)));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            response = httpclient.execute(httppost,responseHandler);       


        } catch (Exception e) {  
            System.out.println(e.getMessage());

        }
        return response;  
    }

Error in Android

at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)

【问题讨论】:

  • 尝试注释掉 System.out.println 看看会发生什么,如果出现错误,请发布整个 logcat 消息。
  • 请添加完整的 logcat,而不是 System.out.println(e.getMessage()); 使用 Log.e(e.toString);

标签: php android web-services


【解决方案1】:

请使用 HTTP GET 方法而不是 HTTP POST,并且 HTTP GET 方法的 url 应该是。

http://10.0.2.2/dreamhotel/deletedetail.php?id=1

并做一些改变服务器端:用户服务器端 $_GET 或 $_REQUEST 而不是 $_POST。

见下文,我编辑了您的代码。

<?php
include "config.php";

//$id = $_POST['id'];
$id = $_GET['id'];

$sql1="delete from detail where id ='$id'";

$result=mysql_query($sql1);

 if($result)
{
  echo"OK";
}
  else
{
  echo "Error";

}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2023-03-24
    相关资源
    最近更新 更多