Android App Development and Web Server Interactions

后端 未结 4 1620
轻奢々
轻奢々 2021-01-01 07:45

I am just learning about Android Development so excuse me if this is a bit off in nature. I am wanting to make an app that interacts with a Database from my website, in a se

相关标签:
4条回答
  • 2021-01-01 07:54

    I'm sure there are libraries out there for Android that help you with HTTP Get and Post, however, if you really want to understand what is going there are just a couple of classes you have to understand in order to make the necessary classes yourself.

    First, get to know HttpClient, HTTPGet, HTTPPost, and HTTPResponse. Some of the later versions of Android have some nice other classes as well, but those four is pretty much all you need to get started.

    You need to do something like this:

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet("http://www.myurl.com/api_name");
    HttpResponse response = client.execute(request);
    

    If you debug this (with a real URL of course), you'll notice that your app kind of freezes during client.execute(). This is the point at which the request has actually fired and the app is waiting for a response. Once you actually get the response, it isn't very difficult to get the data out of it.

    Once you understand this, you'll want to get to know AsyncTask, which is endlessly useful for performing background tasks. You can find the documentation here: http://developer.android.com/reference/android/os/AsyncTask.html There is a great example of how to use this right at the top.

    Using these two concepts together you can perform asynchronous HTTP requests. Basically, put your actual HTTP execute code in doInBackground of your AsyncTask. At the end of the doInBackground return your response, and then do what you want with your data in the AsyncTask's onPostExecute.

    0 讨论(0)
  • 2021-01-01 08:06

    We've found that providing a proper RESTful web API that hits the database on the backend in whatever language you choose (be it PHP, RoR, whatever) provides a useful interface for any number of uses (your own website, mobile apps, etc).

    Then it's a matter of your Android app interacting with the RESTful API, which is simply HTTP requests. Those can be encapsulated in helper classes to make them straightforward as well.

    0 讨论(0)
  • 2021-01-01 08:12

    The approach you mention in the question: PHP on the server and JSON for requests/responses, does work. But getting it perfect can be tricky.

    I found it helpful to have small request/reponse classes for each call on the Android side, like SaveNoteToServerRequest, SaveNoteToServerResponse classes which are just plain java objects with whatever fields are needed for the request/response. Then you can use a library like GSON to convert the request object to JSON and convert the http response from JSON to the response object.

    On the PHP side you can create a small class for the response object, then json_encode at the end.

    That way you're not directly manipulating JSON objects, just using your own plain java objects or php classes most of the time.

    Hope that helps.

    0 讨论(0)
  • 2021-01-01 08:18

    Based on my experience, the best framework for doing RESTFul things with Android is: Spring Android

    From a client perspective, it provides all the tools needed to access secure RESTFul services. Since it is Spring, it provides nice abstractions over most of the boiler plate http code. As an example, it provides a clean way to perform a GET that returns json, and then serialize that to a POJO.

    As an example:

    RestTemplate restTemplate = new RestTemplate();
    
    // Add Jackson JSON Message Converter to Template
    restTemplate.setMessageConverters(
        new ArrayList<HttpMessageConverter<?>>() {
            {              
                add(new MappingJacksonHttpMessageConverter());
            }
        }
    );  
    
    // Simple Conversion - pojo is now populated
    MyPojo pojo = restTemplate.getForObject(url, MyPojo.class);
    
    0 讨论(0)
提交回复
热议问题