开发者社区> 问答> 正文

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

Android 3.3 API 18 你好,

我正在使用android开发我的第一个应用程序。该应用程序将必须连接到在线数据库以存储用户数据。

我正在寻找包含MySQL数据库的云存储。但是,我的App可以直接连接到该MySQL数据库并从中推入和拉出数据吗?还是我需要做其他事情?

非常感谢您的任何建议,

展开
收起
保持可爱mmm 2020-05-17 09:33:51 728 0
1 条回答
写回答
取消 提交回答
  • 是的,你可以这么做。

    您需要的材料:

    网络服务器 Web服务器中存储的数据库 还有一点Android知识:) Webservices(json,Xml ... etc)随便您如何使用 1.首先在清单文件中设置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。来源:stack overflow

    2020-05-17 09:36:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
搭建电商项目架构连接MySQL 立即下载
搭建4层电商项目架构,实战连接MySQL 立即下载
PolarDB MySQL引擎重磅功能及产品能力盛大发布 立即下载

相关镜像