如何Android应用程序连接到MySQL数据库

如何Android应用程序连接到MySQL数据库

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

问题描述

我是新android开发。我有一个网站已经建立的,它使用的MySQL数据库。我想知道我怎么能我的应用程序连接到数据库。

我想实现的是我的应用程序应该要求找到一个定义的ID或名称的表。表中包含链接图像或图像名称。我想该应用检索这些图像,并在Android应用程序中显示。

哪些方法可以做到这一点?我可以用PHP来开发Android应用程序?

如果有哪位朋友能帮助我,建议我的方式或给我的教程适合的链接,可以帮助,我会很感激!

干杯

解决方案
  1. 在你的清单文件将网​​络的权限:

     <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
     

  2. 请一类,使一个HTTP请求到服务器。例如(我使用JSON解析得到的值):

     公共类JSONfunctions {
      公共静态的JSONObject getJSONfromURL(字符串URL){
        InputStream的是= NULL;
        字符串结果=;
        JSONObject的jArray = NULL;
    
        //从网址下载JSON数据
        尝试 {
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(URL);
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();
    
        }赶上(例外五){
            Log.e(log_tag,错误的HTTP连接+ e.toString());
        }
    
        //转换响应串
        尝试 {
            的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\ N);
            }
            is.close();
            结果= sb.toString();
        }赶上(例外五){
            Log.e(log_tag,错误转换结果+ e.toString());
        }
    
        尝试 {
            jArray =新的JSONObject的(结果);
        }赶上(JSONException E){
            Log.e(log_tag,错误分析数据+ e.toString());
        }
    
        返回jArray;
      }
    }
     

  3. 在你的 MainActivity ,使对象的类 JsonFunctions ,并通过从您的网址想要得到的数据作为参数。例如:

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

  4. 然后终于看完了JSON标签和存储在一个ArrayList中的值。后来,他们展示的,如果你想一个ListView。

I am new to android development. I have a website already setup which uses mysql database. I want to know how can i connect my app to that database.

What i wanna achieve is that my app should make a request to find a table of a defined "ID" or name. The table contains links to images or image names. I want the app to retrieve those images and display them on the android app.

What are the ways this can be done?Can i use pHp to develop an android app?

If anyone can also help me by suggesting me a way or giving me suitable links of tutorials that can help, I'll be thankful!

Cheers

解决方案
  1. Set the INTERNET permission in your manifest file:

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

  2. Make a class to make an HTTP request to the server. For example (I am using JSON parsing 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. In your MainActivity, make an object with the class JsonFunctions and pass the URL from where you want to get the data as an argument. For example:

    JSONObject jsonobject;
    jsonobject = JSONfunctions.getJSONfromURL("http://YOUR_DATABASE_URL");
    

  4. And then finally read the JSON tags and store the values in an ArrayList. Later, show them in a ListView if you want.

这篇关于如何Android应用程序连接到MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:54