score:1

connecting android to server using php is best way ...

first use name value pair

        public void registeruser(string email, string password, string mobile) {
    // building parameters

           list<namevaluepair> params = new arraylist<namevaluepair>();

            params.add(new basicnamevaluepair("tag", register_tag));
    params.add(new basicnamevaluepair("keyemail", email));
    params.add(new basicnamevaluepair("keypassword", password));
    params.add(new basicnamevaluepair("keymobile", mobile));

    // getting json object


    jsonparser.makehttprequest(registerurl,params);


}

use json to send the data

      public jsonobject makehttprequest(string url,
        list<namevaluepair> params) {


    // making http request
    try {




            // request method is post
            // defaulthttpclient
            defaulthttpclient httpclient = new defaulthttpclient();
            httppost httppost = new httppost(url);
            httppost.setentity(new urlencodedformentity(params));

            httpresponse httpresponse = httpclient.execute(httppost);
            httpentity httpentity = httpresponse.getentity();
            is = httpentity.getcontent();





    } catch (unsupportedencodingexception e) {
        e.printstacktrace();
    } catch (clientprotocolexception e) {
        e.printstacktrace();
    } catch (ioexception e) {
        e.printstacktrace();
      }

    try {

        bufferedreader reader = new bufferedreader(new inputstreamreader(
                is, "iso-8859-1"), 8);
        stringbuilder sb = new stringbuilder();
        string line = null;
        while ((line = reader.readline()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.tostring();


    } catch (exception e) {
        log.e("buffer error", "error converting result " + e.tostring());
    }

    // try parse the string to a json object
    try {

        jobj = new jsonobject(json);
        log.d("parser", "in try parse the string to a json object");
    } catch (jsonexception e) {
        log.e("json parser", "error parsing data " + e.tostring());
    }

    // return json string
    return jobj;

}

php code will get this value and use further

  // include db connect class
  require_once dirname(__file__).'/db_connect.php';

  // connecting to db
  $db = new db_connect();

  $response = array();



  $email = $_post['keyemail'];
  $password= $_post['keypassword'];
 $phone=$_post['keymobile'];
 $tag=$_post['tag'];



$result=mysql_query("insert into tablename (register_email,password,mobile)    values('$email','$password', '$phone')");

if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "data inserted successfully.";

    // echoing json response
    echo json_encode($response);
} else {
    $response["error_msg"]="error in insertion";
}

?>


Related Query

More Query from same tag