Implement client upload data to server side
- Build the server, create a new database;
- new PHP file;
- New Android Project;
1. Install and provision after downloading the XAMPP software locally.
Enter in the browser: localhost or 127.0.0.1 to see the xampp Welcome screen.
- Modifying the phpMyAdmin configuration
First Open with the relevant software located ind:/xampp/phpmyadmin (installation directory)In the folderconfig.inc.phpFile. Search$cfg [' Servers '] [$i] [' auth_type ']= ' config 'Put one of the config(system default) changes to cookie save.
Enter the
phpMyAdmin interface via the phpMyAdmin navigation bar on the left side of the default home page, and you can enter the Web landing interface directly via localhost/phpmyadmin/ . Enter the user name root and click Login directly (password default is empty).
- New Admin account and password
first go to the Permissions tab, click on the root account to edit its permissions, at the bottom of the only enter the appropriate user name and password, the remaining options to maintain their default values, confirmed that the original root account can be created with the same permissions of the new account
- test whether the configuration is complete
You can create any one of the sites under the Htdocs folder in the Xampp directory . For example, by placing the test file test.html under the . \xampp\htdocs\new Path, you can access the file by entering localhost/new/test.html in the address bar of the browser.
2. Create a new database on the server
- Browser Open localhost/phpmyadmin/ or click the MySQL admin button on the XAMPP Control panel to enter the Web page
- Click on the "Database" tab to create a data cry, enter the database name.
- Click New database on the left side of the page to create a new data table. Enter the name and number of fields for the new data table and click Execute to create it.
3. Create a new PHP file
<?php
$conn = mysql_connect ("localhost". " Siguoyi "," 140265 "); //connection server
if (!conn) {
Die
("conn error");
}
$db = mysql_select_db ("Buptant", $conn); //Connect to database
mysql_set_charset ("UTF8");
$jsonString = $_post ("jsonstring");
$jsonString = str_replace (' \ "', '" ', $jsonString);
$obj = Json_decode ($jsonString);
$name = $obj->name;
$age = $obj->age;
$sex = $obj->sex;
$school = $obj->school; //Assign a value to a variable
try {
$sql = "INSERT into ' student ' (' name ', ' age ', ' sex ', ' school ') VALUES (' $name ', ' $age ', ' $sex ', ' $school ')"; //Insert Data
$resultNew = mysql_query ($sql, $conn);
if (! $resultNew) {
echo "Error";
}else{
echo "OK";
} //query data is inserted
$insert _id = mysql_insert_id ();
} catch (Exception $e) {}
?>
4. New Android Project
- Use JSON to package and upload data
String name = Et_name.gettext (). toString ();
String age = Et_age.gettext (). toString ();
String sex = Et_sex.gettext (). toString ();
String School = Et_school.gettext (). toString ();
try {
jsonobject.put ("name", name);
jsonobject.put ("age", age);
jsonobject.put ("Sex", sex);
Jsonobject.put ("school", school);
} catch (Jsonexception e) {
E.printstacktrace ();
}
log.v ("Uploaddata", "json" + jsonobject);
- Use HttpClient for communication
Public
String Updata () {
String ResultId = "1";
list<namevaluepair> Namevaluepair = new arraylist<namevaluepair> ();
Namevaluepair.add (New Basicnamevaluepair ("jsonstring", Jsonobject));
InputStream inputstream = null;
try {
HttpClient HttpClient = new Defaulthttpclient ();
HttpPost httppost = new HttpPost (Uploaddataurl);
httppost.setentity (New urlencodedformentity (Namevaluepair, "UTF-8"));
HttpResponse HttpResponse = Httpclient.execute (HttpPost);
httpentity httpentity = httpresponse.getentity ();
InputStream = Httpentity.getcontent ();
BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream));
String line = "";
String result = "";
While
(line = Reader.readline ()) = null) {
result = result + line;
}
ResultId = result;
} catch (Exception e) {
E.printstacktrace ();
}
return resultid;
}
- A new thread is required for uploading data
New Thread () {
@Override
Public
void Run () {
//TODO auto-generated method stub
Uploaddata upload = new Uploaddata (URL, jsonobject);
String result = Upload.updata ();
log.v ("Uploaddata", "Result:" + result);
}
}.start ();
Android Clienttosever Test