HTTP POST请求到PHP服务器Android Studio

时间:2016-04-30 08:34:59

标签: android

公共类MainActivity扩展了AppCompatActivity {

<code>
 if (isset($_POST['search'])) {
    $projectName     = $_POST['pName'];
    $clientId      = $_POST['s_by_clientName'];
    $departmentId  = $_POST['s_by_department'];
    $statusName  = $_POST['s_by_status'];
    $my_sql = "select * from project_list "; 
    $my_where = "";
    if (!empty($projectName)) 
    {
        if ($my_where = ""){
            $my_sql .= "where ";
        } else {
            $my_sql .= "and ";
        }
        $my_sql .= "projectName='$projectName'";

    }
    if (!empty($clientId)) 
    {

        if ($my_where = ""){
            $my_sql .= "where ";
        } else {
            $my_sql .= "and ";
        }
        $my_sql .=  "client_id='$clientId'";

    }
    if (!empty($departmentId)) 
    {
        if ($my_where = ""){
            $my_sql .= "where ";
        } else {
            $my_sql .= "and ";
        }
        $my_sql .=  "department_id='$departmentId'";


    }
    if (!empty($statusName)) 
    {
        if ($my_where = ""){
            $my_sql .= "where ";
        } else {
            $my_sql .= "and ";
        }
        $my_sql .=  "status='$statusName'";

    }
}

}

当我点击它显示的发送按钮时,我不知道代码有什么问题。遗憾的是,HTTP已停止(看起来就像应用程序崩溃一样)。  请查看我的代码并告诉我我做错了什么。

3 个答案:

答案 0 :(得分:1)

我立刻看到的一件事是你在主UI线程(onCreate)中提出请求。这是不允许的,因为网络连接通常需要一些时间才能完成。在logcat中也应该有一条关于此的错误消息。

您应该做的是在单独的帖子中提出您的请求。您可以使用AsyncTaskThread来完成此操作。谷歌吧。

UPD:

如何在Java中使用Threads的示例。方法run将异步执行。

new Thread(new Runnable(){
  @Override
  public void run(){
    GetText();
  }
}).start();

下次,请在问题中包含logcat错误。

答案 1 :(得分:0)

考虑使用http://square.github.io/retrofit/进行网络通信。

您将获得一个简单的api,所有请求都将被包装到正确的线程中。

答案 2 :(得分:0)

您可以使用-c

完成此任务