如何使用JSON连接Android和PHP Mysql数据库

如何使用JSON连接Android和PHP Mysql数据库,第1张

Android客户端直接连接远程MySQL数据库的方法如下:String result = ""//首先使用NameValuePair封装将要查询的年数和关键字绑定ArrayList<NameValuePair>nameValuePairs = new ArrayList<NameValuePair>()nameValuePairs/getAllPeopleBornAfter.php")httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))HttpResponse response = httpclient.execute(httppost)HttpEntity entity = response.getEntity()InputStream is = entity.getContent()}catch(Exception e){Log.e("log_tag", "Error in http connection "+e.toString())}//将HttpEntity转化为Stringtry{BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8)StringBuilder sb = new StringBuilder()String line = nullwhile ((line = reader.readLine()) != null) {sb.append(line + "\n")}is.close()result=sb.toString()}catch(Exception e){Log.e("log_tag", "Error converting result "+e.toString())}//将String通过JSONArray解析成最终结果try{JSONArray jArray = new JSONArray(result)for(int i=0i<jArray.length()i++){JSONObject json_data = jArray.getJSONObject(i)Log.i("log_tag","id: "+json_data.getInt("id")+", name: "+json_data.getString("name")+", sex: "+json_data.getInt("sex")+", birthyear: "+json_data.getInt("birthyear"))}}}catch(JSONException e){Log.e("log_tag", "Error parsing data "+e.toString())}虽然Android开发中可以直接连接数据库,但是实际中却不建议这么做,应该使用服务器端中转下完成。

直接读写文件,再把读出来的文件内容格式化成json,再用JDBC、Mybatis或者其他框架将json数据存入数据库。

假设实体类是这样的:

public class ElectSet {

public String xueqi

public String xuenian

public String startTime

public String endTime

public int menshu

public String isReadDB

//{"xueqi":,"xuenian":,"startTime":,"endTime":,"renshu":,"isReadDB":}

public String getXueqi() {

return xueqi

}

public void setXueqi(String xueqi) {

this.xueqi = xueqi

}

public String getXuenian() {

return xuenian

}

public void setXuenian(String xuenian) {

this.xuenian = xuenian

}

public String getStartTime() {

return startTime

}

public void setStartTime(String startTime) {

this.startTime = startTime

}

public String getEndTime() {

return endTime

}

public void setEndTime(String endTime) {

this.endTime = endTime

}

public int getMenshu() {

return menshu

}

public void setMenshu(int menshu) {

this.menshu = menshu

}

public String getIsReadDB() {

return isReadDB

}

public void setIsReadDB(String isReadDB) {

this.isReadDB = isReadDB

}

}

有一个json格式的文件,存的信息如下:

Sets.json:

{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}

具体 *** 作:

/*

* 取出文件内容,填充对象

*/

public ElectSet findElectSet(String path){

ElectSet electset=new ElectSet()

String sets=ReadFile(path)//获得json文件的内容

JSONObject jo=JSONObject.fromObject(sets)//格式化成json对象

//System.out.println("------------" jo)

//String name = jo.getString("xuenian")

//System.out.println(name)

electset.setXueqi(jo.getString("xueqi"))

electset.setXuenian(jo.getString("xuenian"))

electset.setStartTime(jo.getString("startTime"))

electset.setEndTime(jo.getString("endTime"))

electset.setMenshu(jo.getInt("menshu"))

electset.setIsReadDB(jo.getString("isReadDB"))

return electset

}

//设置属性,并保存

public boolean setElect(String path,String sets){

try {

writeFile(path,sets)

return true

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

return false

}

}

//读文件,返回字符串

public String ReadFile(String path){

File file = new File(path)

BufferedReader reader = null

String laststr = ""

try {

//System.out.println("以行为单位读取文件内容,一次读一整行:")

reader = new BufferedReader(new FileReader(file))

String tempString = null

int line = 1

//一次读入一行,直到读入null为文件结束

while ((tempString = reader.readLine()) != null) {

//显示行号

System.out.println("line " line ": " tempString)

laststr = laststr tempString

line

}

reader.close()

} catch (IOException e) {

e.printStackTrace()

} finally {

if (reader != null) {

try {

reader.close()

} catch (IOException e1) {

}

}

}

return laststr

}

将获取到的字符串,入库即可。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9716569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存