new Insert_to_Database().execute();
class Insert_to_Database extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Upload.this);
pDialog.setMessage("Saving the data.....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a",
Locale.getDefault());
String timeStamp1 = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
String imageFileName1 = "JPEG_" + timeStamp1 + "_";
String time = sdf.format(cal.getTime());
String name = Name.getText().toString();
String image = "uploads/" + imageFileName1 + ".jpg";
String Photo = String
.valueOf(((System.currentTimeMillis() / 1000) / 60));
nvp = new ArrayList<NameValuePair>();
List<MyObject> contacts_insert = db1.getAllContacts(Name.getText()
.toString());
int rowCount = contacts_insert.size();
String[] item = new String[rowCount];
int x = 0;
for (MyObject cn : contacts_insert) {
item[x] = cn.getName();
x++;
nvp.add(new BasicNameValuePair("ID", String.valueOf(cn.getID())));
nvp.add(new BasicNameValuePair("Photo", "uploads/" + cn.getID()
+ ".jpg"));
}
Log.d("//////", "" + time);
nvp.add(new BasicNameValuePair("Name", name));
nvp.add(new BasicNameValuePair("Time", time));
nvp.add(new BasicNameValuePair("Imei", imei));
pDialog.show();
}
/**
*
* Creating product
*
* */
protected String doInBackground(String... args) {
InputStream inputStream = null;
JSONArray jArraych = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_create_product);
httpPost.setEntity(new UrlEncodedFormEntity(nvp));
Log.e("URL : ", url_create_product.toString());
Log.e("NVP : ", nvp.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// Read content & Log
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
// Convert response to string using String Builder
try {
BufferedReader bReader = new BufferedReader(
new InputStreamReader(inputStream, "iso-8859-1"), 8);
// BufferedReader bReader = new BufferedReader(
// new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
String result = sBuilder.toString();
Log.e("RESULT : ", result);
if (result.trim().equals("1")) {
Log.d("Result from web : ", "SUCCESS : " + result);
} else {
Log.d("Result from web : ", "Failed : " + result);
Toast.makeText(getApplicationContext(), "Upload Failed",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.e("StringBuilding & BufferedReader",
"Error converting result " + e.toString());
}
return null;
}
/**
*
* After completing background task Dismiss the progress dialog
*
* **/
@Override
protected void onPostExecute(String res) {
try {
pDialog.dismiss();
}
}
Config.php
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user //postgres
define('DB_PASSWORD', " "); // db password (mention your db password here)
define('DB_DATABASE', "db_name"); // database name //postgis
define('DB_SERVER', "localhost"); // db server
?>
DbConnect.php
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
include 'Config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
insert.php
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
$name = $_POST['Name'];
$description = $_POST['Description'];
echo $name;
echo $description;
// include db connect class
include 'db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$query="INSERT INTO details(Name, Description) VALUES('$name', '$description')";
echo $query;
$result = mysql_query($query);
// check if row inserted or not
echo $result;
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
?>