最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android通過(guò)json向MySQL中讀寫數(shù)據(jù)的方法詳解【寫入篇】

 更新時(shí)間:2016年06月30日 16:08:11   作者:天鷹之眼  
這篇文章主要介紹了Android通過(guò)json向MySQL中讀寫數(shù)據(jù)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android json類的定義、調(diào)用及php接收json數(shù)據(jù)并寫入mysql的實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android通過(guò)json向MySQL中寫入數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

先說(shuō)一下如何通過(guò)json將Android程序中的數(shù)據(jù)上傳到MySQL中:

首先定義一個(gè)類JSONParser.Java類,將json上傳數(shù)據(jù)的方法封裝好,可以直接在主程序中調(diào)用該類,代碼如下

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST
public JSONObject makeHttpRequest(String url, String method,
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,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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}

主程序中這樣調(diào)用:

params = new ArrayList<NameValuePair>();
//這里可以替換成你自己程序中的一些鍵值對(duì)
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();
//數(shù)據(jù)的php文件的路徑
String url_up = "******/文件名字.php";
try{
JSONObject json = jsonParser.makeHttpRequest(url_up,"POST", params);
Log.v("uploadsucceed", "uploadsucceed");
}catch(Exception e){
e.printStackTrace();
}

最后就是定義一個(gè)接收數(shù)據(jù)的php文件:

<?php
// array for JSON response
//此處需要將數(shù)據(jù)庫(kù)名和表明還有密碼做相應(yīng)修改,改成你自己的
$con = mysql_connect("localhost","root",null);
if (!$con) {
die('Could not connect:'.mysql_error() );
}
mysql_select_db("a0722152915", $con);
$response = array();
include("conn.php");
// check for required fields
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;
// check if row inserted or not
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);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>

注意:如果你的設(shè)備中android操作系統(tǒng)是4.0以上的,那么要在主程序中加上下面一段代碼,才能上傳成功

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
  .detectDiskReads()
  .detectDiskWrites()
  .detectNetwork() // or .detectAll() for all detectable problems
  .penaltyLog()
  .build());
  StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
  .detectLeakedSqlLiteObjects()
  .detectLeakedClosableObjects()
  .penaltyLog()
  .penaltyDeath()
  .build());

如果是4.0以下的操作系統(tǒng)當(dāng)然不用加了

下面是上傳成功后的效果圖:

讀數(shù)據(jù)的方法講放在下一篇《Android通過(guò)json向MySQL中讀寫數(shù)據(jù)的方法詳解【讀取篇】》中介紹

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果

    AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果

    這篇文章主要介紹了AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁(yè)滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android利用zxing生成二維碼的過(guò)程記錄

    Android利用zxing生成二維碼的過(guò)程記錄

    Android中二維碼生成的最常用庫(kù)就是zxing了,正好目前項(xiàng)目有了生成二維碼的需求,所以下面這篇文章主要給大家介紹了關(guān)于Android利用zxing生成二維碼的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Android實(shí)現(xiàn)完整游戲循環(huán)的方法

    Android實(shí)現(xiàn)完整游戲循環(huán)的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)完整游戲循環(huán)的方法,以實(shí)例代碼形式較為詳細(xì)的分析了Android游戲循環(huán)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android?IdleHandler使用方法詳解

    Android?IdleHandler使用方法詳解

    這篇文章主要為大家介紹了Android?IdleHandler使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Flutter圖片加載與緩存機(jī)制的深入探究

    Flutter圖片加載與緩存機(jī)制的深入探究

    應(yīng)用開發(fā)中經(jīng)常會(huì)碰到網(wǎng)絡(luò)圖片的加載,通常我們會(huì)對(duì)圖片進(jìn)行緩存,以便下次加載同一張圖片時(shí)不用再重新下載,下面這篇文章主要給大家介紹了關(guān)于Flutter圖片加載與緩存機(jī)制的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示)

    Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示)

    下面小編就為大家分享一篇Android 實(shí)現(xiàn)帶角標(biāo)的ImageView(微博,QQ消息提示),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼

    Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼

    這篇文章主要介紹了Android自定義View實(shí)現(xiàn)箭頭沿圓轉(zhuǎn)動(dòng)實(shí)例代碼,需要的朋友可以參考下
    2017-09-09
  • Android Studio真機(jī)無(wú)線連接USB設(shè)備調(diào)試運(yùn)行詳解流程

    Android Studio真機(jī)無(wú)線連接USB設(shè)備調(diào)試運(yùn)行詳解流程

    你在Android Studio寫app時(shí)是否也有想過(guò)如果可以不用數(shù)據(jù)線連接手機(jī)調(diào)試運(yùn)行就好了?如果需要取出數(shù)據(jù)線插接的話我肯定是嫌麻煩的,但是模擬器有時(shí)候需要測(cè)試一些需要硬件支持的功能時(shí)又不管用,所以最好的測(cè)試還是在真機(jī)上,本篇教你扔掉數(shù)據(jù)線來(lái)無(wú)線調(diào)試
    2021-11-11
  • android實(shí)現(xiàn)清理緩存功能

    android實(shí)現(xiàn)清理緩存功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)清理緩存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Flutter自定義實(shí)現(xiàn)彈出層的示例代碼

    Flutter自定義實(shí)現(xiàn)彈出層的示例代碼

    這篇文章主要為大家詳細(xì)介紹了Flutter如何自定義組件實(shí)現(xiàn)彈出層的效果,?文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-08-08

最新評(píng)論

华宁县| 井研县| 屯门区| 鲁山县| 朔州市| 攀枝花市| 蒙山县| 金沙县| 甘洛县| 昆明市| 台东市| 兴安县| 陆川县| 荆州市| 长沙市| 方正县| 开原市| 庆阳市| 区。| 景泰县| 承德市| 墨竹工卡县| 尼玛县| 东安县| 浙江省| 韩城市| 青海省| 定西市| 盱眙县| 介休市| 洛隆县| 泰州市| 正定县| 兰考县| 莎车县| 钟祥市| 海晏县| 望江县| 忻州市| 武夷山市| 东海县|