android 手机怎么连接电脑上的数据库,直接获取数据库数据

android 手机怎么连接电脑上的数据库,直接获取数据库数据,第1张

数据库很多,但是安卓直接支持的数据库只有sqlite一种。如果要使用其它的非本地数据库,你得建立连接,采用webservice或http协议中转数据。

安卓访问mysql里的数据得:

1)先建立网络,使用WIFI局域网

2)在PC建立数据访问接口,编写http协议应用(方法非常多。 asp、php、jsp都有访问mysql的方法),还得关掉防火墙

3)编写安卓手机客户端连接设备的http service

下面介绍三种获取网页数据的代码

例子来自于android学习手册,android学习手册包含9个章节,108个例子,源码文档随便看,例子都是可交互,可运行,源码采用android studio目录结构,高亮显示代码,文档都采用文档结构图显示,可以快速定位。360手机助手中下载,图标上有贝壳

//第一种

/**获取参数(ArrayList<NameValuePair>nameValuePairs,String url)后post给远程服务器

* 将获得的返回结果(String)返回给调用者

* 本函数适用于查询数量较少的时候

*/

public String posturl(ArrayList<NameValuePair>nameValuePairs,String url){

String result = ""

String tmp= ""

InputStream is = null

try{

HttpClient httpclient = new DefaultHttpClient()

HttpPost httppost = new HttpPost(url)

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))

HttpResponse response = httpclient.execute(httppost)

HttpEntity entity = response.getEntity()

is = entity.getContent()

}catch(Exception e){

return "Fail to establish http connection!"

}

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()

tmp=sb.toString()

}catch(Exception e){

return "Fail to convert net stream!"

}

try{

JSONArray jArray = new JSONArray(tmp)

for(int i=0i<jArray.length()i++){

JSONObject json_data = jArray.getJSONObject(i)

Iterator<?>keys=json_data.keys()

while(keys.hasNext()){

result += json_data.getString(keys.next().toString())

}

}

}catch(JSONException e){

return "The URL you post is wrong!"

}

return result

}

//第二种

/**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析

* 返回String

*/

public String posturl(String url){

InputStream is = null

String result = ""

try{

HttpClient httpclient = new DefaultHttpClient()

HttpPost httppost = new HttpPost(url)

HttpResponse response = httpclient.execute(httppost)

HttpEntity entity = response.getEntity()

is = entity.getContent()

}catch(Exception e){

return "Fail to establish http connection!"+e.toString()

}

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()

result=sb.toString()

}catch(Exception e){

return "Fail to convert net stream!"

}

return result

}

//第三种

/**获取指定地址的网页数据

* 返回数据流

*/

public InputStream streampost(String remote_addr){

URL infoUrl = null

InputStream inStream = null

try {

infoUrl = new URL(remote_addr)

URLConnection connection = infoUrl.openConnection()

HttpURLConnection httpConnection = (HttpURLConnection)connection

int responseCode = httpConnection.getResponseCode()

if(responseCode == HttpURLConnection.HTTP_OK){

inStream = httpConnection.getInputStream()

}

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

return inStream


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

原文地址: https://outofmemory.cn/sjk/10827294.html

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

发表评论

登录后才能评论

评论列表(0条)

保存