简体   繁体   English

如何将我的Android应用程序连接到我的PHP / MySQL后端?

[英]How to connect my Android app to my PHP/MySQL backend?

I have two questions about the connection between Android and PHP/MySQL. 关于Android与PHP / MySQL之间的连接,我有两个问题。

  1. If I used version 3 and above is that true that I need to do the connection in background using a separate thread? 如果我使用的是版本3和更高版本,那是真的,我需要使用单独的线程在后台进行连接吗?

  2. Is it necessary to use JSON to get back the answers? 是否有必要使用JSON来获取答案?

I wrote code without using multi-threading and JSON but it only works on the version of 2.3 and above. 我编写的代码未使用多线程和JSON,但仅适用于2.3及更高版本。 I tried on the 4.0 and 4.2, but it didn't give back any response. 我在4.0和4.2上进行了尝试,但没有给出任何响应。

Your first question: 您的第一个问题:

Yes. 是。 Always do network tasks or anything else that takes time in the background. 始终在后台执行网络任务或其他任何需要花费时间的事情。 The best way to do this would be to use an AsyncTask . 最好的方法是使用AsyncTask This article explains AsyncTask way better than I can, go read it. 本文以比我更好的方式解释了AsyncTask ,请继续阅读。

Contrary to what the comments on your question say, the reason you should use a separate thread is not because you'd get a NetworkOnMainThreadException otherwise. 与对问题的评论相反,您应该使用单独的线程的原因不是因为否则会得到NetworkOnMainThreadException It's because it's a better practice and because it makes sure your app doesn't stutter while it does the network task. 这是因为这是一种更好的做法,并且可以确保您的应用在执行网络任务时不会卡顿。 The main task also handles animations etc. in your Activity , so doing any task on the main thread for X time, means the app stutters for X time. 主要任务还可以处理Activity动画等,因此在主线程上执行任何任务X倍,这意味着应用程序在X时间内停顿了。

Your second question: 您的第二个问题:

No, it isn't necessary to use JSON. 不,没有必要使用JSON。 You do want to route your requests through a script on your webpage though (whether that'd be PHP, Ruby, Python, whatever), instead of interfacing with your database directly. 您确实希望通过网页上的脚本(无论是PHP,Ruby,Python等)来路由请求,而不是直接与数据库交互。 This way, you can restrict the actions your app is able to perform, as well as the actions a potential hacker is able to perform. 这样,您可以限制您的应用程序可以执行的操作,以及潜在黑客可以执行的操作。

Like I said, it is not necessary to use JSON. 就像我说的那样,没有必要使用JSON。 However, it's the most widely accepted way to get information from your server to your app, for a couple of reasons. 但是,出于多种原因,这是从服务器到应用程序获取信息的最广泛接受的方法。 The most prevalent 2 being: 最普遍的2是:

  1. Low overhead : JSON uses very little 'extra' characters between your data, as opposed to, for example, XML, which has long tags, et cetera; 低开销 :JSON在您的数据之间仅使用很少的“多余”字符,而XML带有较长的标签等。
  2. Ease of use : Android has JSON tools built in for you to leverage, which makes it very easy for you to work with JSON. 易用性 :Android内置了JSON工具供您使用,这使您使用JSON变得非常容易。 For example, take this bit of JSON: 例如,使用以下JSON:

[{'id':11,'name':'Bob'},{'id':42,'name':'Sally'}]

To parse this in your Android app, you could do: 要在您的Android应用中对此进行解析,您可以执行以下操作:

public List<Person> parseJson(String jsonString) {

    // Initialize the ArrayList we're gonna store the people in
    List<Person> people = new ArrayList<Person>();

    try {
        // Convert the JSON from text (String) to a JSON Array, so we can
        // more easily traverse it
        JSONArray rootArray = new JSONArray(jsonString);

        // loop through the prople in the JSON Array
        for(int i=0; i<rootArray.length(); 

            // Get the object at position i from the JSON Array
            JSONObject workingObj = rootArray.get(i);

            // Do what you have to to store the data. In this example,
            // I'm using a class called 'Person' which has setters for Id and Name
            Person p = new Person();

            // Get all the info you need from the JSON Object. As you can see
            // in the JSON snippet, we have an integer with key 'id' and a
            // string with key 'name'
            p.setId(workingObj.getInt("id"));
            p.setName(workingObj.getString("name"));

            // add the Person p to the ArrayList
            people.add(p);
        }
    } catch (JSONException e) {
        // properly handle all exceptions!
    }
    return people;
}

As you can see, all parsing is done for you, and you just have to accommodate for the data structure. 如您所见,所有解析已为您完成,您只需要适应数据结构即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM