标签:
实现客户端上传数据到服务器端
在浏览器里输入:localhost或者127.0.0.1即可看到XAMPP欢迎界面。
通过默认主页localhost左侧的phpmyadmin导航栏进入phpmyadmin界面,可直接通过localhost/phpmyadmin/进入web登陆界面,输入用户名root后直接点击登陆(密码默认为空)即可。
首先进入权限选项卡,点击root帐号编辑其权限,在最底端的只输入相应的用户名和密码,其余选项保持其缺省值,确认后即可产生与原有root帐号相同权限的新帐号
您可以在xampp目录下的htdocs文件夹下创建任意一个站点。例如:将测试文件test.html放在.\xampp\htdocs\new路径下,您就可以在浏览器的地址栏中输入localhost/new/test.html来访问这个文件。
2. 在服务器上新建数据库
<?php$conn = mysql_connect("localhost"."siguoyi","140265"); // 连接服务器if(!conn){die("conn error");}$db = mysql_select_db("buptant",$conn); // 连接数据库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; // 对变量进行赋值try {$sql = "INSERT INTO `student` (`name`,`age`,`sex`,`school`) VALUES (‘$name‘,‘$age‘,‘$sex‘,‘$school‘)"; // 插入数据$resultNew = mysql_query($sql,$conn);if(!$resultNew){echo "error";}else{echo "ok";} // 查询数据是否插入$insert_id = mysql_insert_id();} catch (Exception $e){ }?>
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);
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;}
new Thread(){@Overridepublic void run() {// TODO Auto-generated method stubUploadData upload = new UploadData(Url, jsonObject);String result = upload.upData();Log.v("uploaddata", "result:" + result);}}.start();
标签:
原文地址:http://www.cnblogs.com/siguoyi/p/4346343.html