繁体   English   中英

如何创建示例Web服务以将json格式的客户端android的php中的值插入数据库

[英]How to create sample webservices for insert values into database in php for client android in json format

我需要创建php webservices是将值存储到来自android(client)的数据库中。 一些帮助创建Web服务的示例(例如服务器中的用户名和密码存储)

我是这样创建的。现在问题是值未插入数据库。 这是我的代码

<?php
$_SERVER['REQUEST_METHOD'] == "POST";
    //Connect to the database
$username = "indroids_admin";
$password = "infive";
$hostname = "localhost"; 
$dbname = "indroids_CallLog";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password);  //, $dbname
 //Select the database
// mysql_select_db($dbname);

 //Method to verify the users login

if (!$dbhandle) {
    die('Could not connect: ' . mysql_error());
} 
  //  $username=$_REQUEST['username'];
   //  $password=$_REQUEST['password'];

    $username="aa";
    $password="bb";

    // Insert data into data base

    $sql = "INSERT INTO `Data` (`id`,`name`) VALUES ('$username', '$password');";//('$username', '$password')
        //$sql = "INSERT INTO Data (id, name) VALUES ('$username', '$password');";//('$username', '$password')        

          mysql_select_db($dbname);
        //  $qur = $conn->query($sql)                    //Object-oriented
         $qur  = mysql_query( $sql, $dbhandle );     //Procedural
    //$qur  =$conn->exec($sql);                    //PDO
         //$qur = mysql_query($sql);

    if($qur){
        $json = array("1");
    }else{
        $json = array("0");
              //  die('Could not enter data: ' . mysql_error());
    }
@mysql_close($conn);

/* Output header */
    header('Content-type: application/json');
    echo json_encode($json);

?>

它总是返回false ...检查一次

提前致谢..

尝试这个。 码。 这是一个很好的例子。

    <?php

 //Get the name of the Method
 //The method name has to be passed as Method via post

 $Request_Method=$_REQUEST['method'] or die('Method name not found');

 //Connect to the database
 $Connection = mysql_connect("localhost","root","") or die('Cannot connect to Database');

 //Select the database
 mysql_select_db("EmployeeDB") or die('Cannot select Database');

 //Method to verify the users login
 if($Request_Method=="verifyLogin")
 {
  //username and password are password are passed via querystrings
  $username=$_REQUEST['username'];
 $password=$_REQUEST['password'];

 //Generate the sql query based on username and password
 $query="select id from Employees where username='$username' and password='$password'";

 //Execute the query
 $result = mysql_query($query);

 //Get the rowcount
 $rowcount= mysql_num_rows($result);

 //if the count is 0 then no matching rows are found
 if($rowcount==0)
 {
  echo json_encode(array('result'=>0));
 }
 //Else there is an employee with the given credentials
 else {
  $row = mysql_fetch_assoc($result);
  //Get and return his employee id
  echo json_encode(array('result'=>$row['id']));
 }
 }

 //Get all th employees that are managed the by the given emplyee
 if($Request_Method=="getEmployees")
 {
  $id=$_REQUEST['id'];
 $query="select name,address from Employees where manager=$id";

 $result = mysql_query($query);

 while($row = mysql_fetch_assoc($result))
 {
  $resultArray[] = $row;
 }

 echo json_encode($resultArray);
 }

 //Close Connection
 mysql_close($Connection);
?>

Andoid客户端代码

     public Object getResponseObject(ArrayList Parameters,Class c)
 {
  try{
   //Create a HTTP Client
   HttpClient httpclient = new DefaultHttpClient();

   //Create and object to Post values to the server
   //The url is specified in the Constants class to increase modifiability
   HttpPost httppost = new HttpPost(Constants.SERVICE_URL);

   //Set the attributes to be posted as Parameters
   httppost.setEntity(new UrlEncodedFormEntity(Parameters));

   //Execute the post and get the response
   HttpResponse response = httpclient.execute(httppost);

   //Get the response as ans entity
   HttpEntity entity = response.getEntity();

   //Get the content of the response as a stream
   InputStream stream=entity.getContent();

   //Convert the stream to a GSON object
         String result= convertStreamToString(stream);

         //Create a GSON object
         Gson gson=new Gson();

         //Convert the respose string to a object of the given type
         //Here Object class is used so that we can use the same method to get any
         //class's object as response
   Object responseObject=gson.fromJson(result, c);
   //Return the object
         return responseObject;
  }catch(Exception e){
   Log.e("PHP Client", "Error in http connection"+e.toString());
   return null;
  }
 }

定1

定2

暂无
暂无

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

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