先说一下如何通过json将Android程序中的数据上传到MySQL中: 首先定义一个类JSONParser.Java类,将json上传数据的方法封装好,可以直接在主程序中调用该类,代码如下 [java] view plain copy public class JSONParser{ static InputStreamis= null ; static
先说一下如何通过json将Android程序中的数据上传到MySQL中:
首先定义一个类JSONParser.Java类,将json上传数据的方法封装好,可以直接在主程序中调用该类,代码如下
-
public class JSONParser {
-
static InputStream is = null;
-
static JSONObject jObj = null;
-
static String json = "";
-
-
public JSONParser() {
-
}
-
-
-
public JSONObject makeHttpRequest(String url, String method,
-
List<NameValuePair> params) {
-
-
try {
-
-
-
DefaultHttpClient httpClient = new DefaultHttpClient();
-
HttpPost httpPost = new HttpPost(url);
-
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
-
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, "UTF-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());
-
Log.d("json", json.toString());
-
}
-
-
try {
-
jObj = new JSONObject(json);
-
} catch (JSONException e) {
-
Log.e("JSON Parser", "Error parsing data " + e.toString());
-
}
-
-
return jObj;
-
}
-
}
主程序中这样调用:
-
params = new ArrayList<NameValuePair>();
-
-
params.add(new BasicNameValuePair("time", ""+time));
-
params.add(new BasicNameValuePair("lat", ""+lat));
-
params.add(new BasicNameValuePair("lon", ""+lon));
-
params.add(new BasicNameValuePair("encyptiontype",encyptiontype));
-
params.add(new BasicNameValuePair("rssi",rssi));
-
params.add(new BasicNameValuePair("name",name));
-
-
-
JSONParser jsonParser = new JSONParser();
-
数据的php文件的路径
-
String url_up = "******/文件名字.php";
-
-
try{
-
JSONObject json = jsonParser.makeHttpRequest(url_up,"POST", params);
-
Log.v("uploadsucceed", "uploadsucceed");
-
-
}catch(Exception e){
-
e.printStackTrace();
-
}
最后就是定义一个接收数据的php文件:
-
<php
-
-
-
-
$con = mysql_connect("localhost","root",null);
-
if (!$con) {
-
die('Could not connect:'.mysql_error() );
-
}
-
mysql_select_db("a0722152915", $con);
-
-
-
-
-
$response = array();
-
include("conn.php");
-
-
if (isset($_POST['time']) && isset($_POST['lat']) && isset($_POST['lon'])&& isset($_POST['encyptiontype'])&& isset($_POST['rssi'])&& isset($_POST['name'])) {
-
$time = $_POST['time'];
-
$lat = $_POST['lat'];
-
$lon = $_POST['lon'];
-
$encyptiontype = $_POST['encyptiontype'];
-
$rssi = $_POST['rssi'];
-
$name = $_POST['name'];
-
$result = mysql_query("INSERT INTO wifi_state(time, lat, lon,encyptiontype,rssi,name) VALUES('$time', '$lat', '$lon','$encyptiontype','$rssi','$name')");
-
echo $result;
-
-
if ($result) {
-
-
$response["success"] = 1;
-
$response["message"] = "Product successfully created.";
-
-
echo json_encode($response);
-
} else {
-
-
$response["success"] = 0;
-
$response["message"] = "Oops! An error occurred.";
-
-
echo json_encode($response);
-
}
-
} else {
-
-
$response["success"] = 0;
-
$response["message"] = "Required field(s) is missing";
-
-
echo json_encode($response);
-
}
-
>
注意:如果你的设备中android操作系统是4.0以上的,那么要在主程序中加上下面一段代码,才能上传成功
-
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
-
.detectDiskReads()
-
.detectDiskWrites()
-
.detectNetwork()
-
.penaltyLog()
-
.build());
-
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
-
.detectLeakedSqlLiteObjects()
-
.detectLeakedClosableObjects()
-
.penaltyLog()
-
.penaltyDeath()
-
.build());
如果是4.0以下的操作系统当然不用加了
下面是上传成功后的效果图:

读数据的方法放在下一篇介绍