栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android应用程序可以直接连接到在线mysql数据库吗

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android应用程序可以直接连接到在线mysql数据库吗

是的,你可以这么做。

您需要的材料:

  1. 网络服务器
  2. Web服务器中存储的数据库
  3. 还有一点Android知识:)
  4. Webservices(json,Xml … etc)随便您如何使用

1. 首先在清单文件中设置Internet权限

 <uses-permission android:name="android.permission.INTERNET" />

2.创建 一个类以从服务器发出HTTPRequest(我正在使用json parisng获取值)

例如:

    public class JSonfunctions {    public static JSonObject getJSonfromURL(String url) {        InputStream is = null;        String result = "";        JSonObject jArray = null;        // Download JSON data from URL        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) { Log.e("log_tag", "Error in http connection " + e.toString());        }        // Convert response to string        try { BufferedReader reader = new BufferedReader(new InputStreamReader(         is, "iso-8859-1"), 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) { Log.e("log_tag", "Error converting result " + e.toString());        }        try { jArray = new JSonObject(result);        } catch (JSonException e) { Log.e("log_tag", "Error parsing data " + e.toString());        }        return jArray;    }}

3.

MainActivity
“创建类的对象”中
JsonFunctions
,将url作为参数从您要获取数据的位置传递

例如:

JSonObject jsonobject;

jsonobject = JSONfunctions.getJSonfromURL("http://YOUR_DATAbase_URL");

4. 然后,最后读取jsontags并将值存储在arraylist中,如果需要,稍后在listview中显示它

如果您有任何问题,可以关注此博客,他提供了出色的android教程AndroidHive

由于上述答案我写了很长回来,现在

HttpClient
HttpPost
HttpEntity
已在阿比23.删除您可以使用下面的的build.gradle(应用级)的代码仍继续使用
org.apache.http
的项目。

android {    useLibrary 'org.apache.http.legacy'    signingConfigs {}    buildTypes {}}

或者您可以使用

HttpURLConnection
如下所示从服务器获取响应

public String getJSON(String url, int timeout) {HttpURLConnection c = null;try {    URL u = new URL(url);    c = (HttpURLConnection) u.openConnection();    c.setRequestMethod("GET");    c.setRequestProperty("Content-length", "0");    c.setUseCaches(false);    c.setAllowUserInteraction(false);    c.setConnectTimeout(timeout);    c.setReadTimeout(timeout);    c.connect();    int status = c.getResponseCode();    switch (status) {        case 200:        case 201: BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) {     sb.append(line+"n"); } br.close(); return sb.toString();    }} catch (MalformedURLException ex) {    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);} catch (IOException ex) {    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);} finally {   if (c != null) {      try {          c.disconnect();      } catch (Exception ex) {         Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);      }   }}return null;

}

或您可以使用第三方库(如

Volley
Retrofit
来调用webservice api并获取响应,然后使用
FasterXML-jackson
,解析
google-gson



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/402729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号