一个Android应用程序可以直接连接到网上的MySQL数据库 [英] Can an Android App connect directly to an online mysql database

查看:222
本文介绍了一个Android应用程序可以直接连接到网上的MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 3.3 API 18

您好,

我发展我的第一个应用程序使用的Andr​​oid。的应用将具有连接到一个在线数据库来存储用户数据。

I am developing my first App using android. The App will have to connect to an online database to store user data.

我要寻找一个云存储与MySQL数据库包括在内。然而,可我的应用程序直接连接到这个MySQL数据库和推动,并从中提取数据。还是有其他的事情,我需要做什么?

I am looking for an cloud storage with a MySql database included. However, can my App connect directly to this MySql database and push and pull data from it. Or is there other things I need to do?

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

是的,你可以做到这一点。

Yes you can do that.

材料需要:

  1. Web服务器
  2. 存储在Web服务器的数据库
  3. 在一点点的android知识:)
  4. Web服务(JSON,XML ...等),无论你是舒服

1 首先设置上网权限在manifest文件

1. First set the internet permissions in your manifest file

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

2 请类作出HTT prequest从服务器 (我使用JSON parisng得到的值)

2. Make a class to make an HTTPRequest from the server (i am using json parisng to get the values)

有关,例如:

    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 和传球的对象网址作为参数,从那里你想要得到的数据

3. In your MainActivity Make an object of the class JsonFunctions and pass the url as an argument from where you want to get the data

例如:

JSONObject jsonobject;

的JSONObject = JSONfunctions.getJSONfromURL(HTTP:// YOUR_DATABASE_URL);

4。,然后终于看完了jsontags并存储在一个ArrayList的价值观,后来在列表视图显示,如果你想

4. And then finally read the jsontags and store the values in an arraylist and later show it in listview if you want

如果你有任何问题,你可以按照这个博客 他给优秀的android教程 AndroidHive

and if you have any problem you can follow this blog he gives excellent android tutorials AndroidHive

这篇关于一个Android应用程序可以直接连接到网上的MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆