An introduction or tutorial to extract data from your own site?

I need to implement a module in an Android application that extracts text content (not displayed in the browser, but processed by the application) from my own site.

This text content will be stored in a database (MySQL?).

My only problem is that I have never done anything like this before (although I am familiar with basic SQL), so I’m not sure what will be a good methodological approach to solving this topic, and I don’t even know where to start.

Should I create a MySQL database from scratch and start coding PHP around it?

Is there an infrastructure or wrapper that is already focused on communicating with Android applications and allows me to focus on the features of my needs?

What would you recommend as a good starting point?

+4
source share
4 answers

You can do this in different ways, you can do something really simple, but as you progress and start sending additional information from your servers to your applications, you need to implement something more reliable.

Although @Robert's answer is valid, I find it much better for a mobile phone to use RESTful web services using JSON.

You can program your web services whatever you want, I would recommend (if you can program server-side in java) to take a look at GSON , which is a great library for using JSON. Using JSON objects, you can convert all the necessary information to a string, and then transfer the mobile phone to a Java object. You can do whatever you want to display it in the user interface or save it to the database.

+3
source

First, you must develop a contact service (WebService, REST, or whatever suits your needs) for your application. In my case, I am using a PHP script that generates a JSON response.

Here is a simple PHP script that connects to the database and outputs JSON ( json_encode method ):

<?php $link = mysqli_connect("localhost", "dbuser", "dbpassword", "database"); $response = array(); $pass = mysqli_query("select * from table"); while($data = mysqli_fetch_object($pass)) { array_push($response, array( "my_field" => $data->my_field )); } echo json_encode($response); mysqli_close($link); 

? >

The script will output the JSON response as follows:

 [{"my_field":1}] 

When your application will request data using an HTTP POST or GET response, you will analyze the response using a library. Since I use JSON on the PHP side, I am currently using GSON for my projects.

To request JSON data from your Android application, you can use RestClass, which I took somewhere (I don't remember), but here is the code:

 public class RestClient { private ArrayList <NameValuePair> params; private ArrayList <NameValuePair> headers; private String url; private int responseCode; private String message; private String response; public enum RequestMethod { GET, POST } public String getResponse() { return response; } public String getErrorMessage() { return message; } public int getResponseCode() { return responseCode; } public RestClient(String url) { this.url = url; params = new ArrayList<NameValuePair>(); headers = new ArrayList<NameValuePair>(); } // ------------------------------------------------------------------------------- // Various methods. // ------------------------------------------------------------------------------- /** * Add a parameter to the request (pair, name/value). * @param name Name of the parameter (ex: &name=) * @param value Value of the parameter (ex: &name=value). */ public void AddParam(String name, String value) { params.add(new BasicNameValuePair(name, value)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** * Add a header to the request (if needed). * @param name Name of the header. (ex: Content-Type) * @param value Value of the header. (ex: Content-Type = text/html). */ public void AddHeader(String name, String value) { headers.add(new BasicNameValuePair(name, value)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** * Fetch the HTTP content via POST or GET. * @param method Method to be used. * @throws Exception Exception to be catched in case of a network problem. */ public void Execute(RequestMethod method) throws Exception { switch(method) { case GET: { //add parameters String combinedParams = ""; if(!params.isEmpty()){ combinedParams += "?"; for(NameValuePair p : params) { String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8"); if(combinedParams.length() > 1) { combinedParams += "&" + paramString; } else { combinedParams += paramString; } } } HttpGet request = new HttpGet(url + combinedParams); //add headers for(NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } executeRequest(request, url); break; } case POST: { HttpPost request = new HttpPost(url); //add headers for(NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } if(!params.isEmpty()){ request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } executeRequest(request, url); break; } } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** * Alternative method to execute the HTTP request. * @param request The HTTP request object to be used. * @param url HTTP url. */ private void executeRequest(HttpUriRequest request, String url) { HttpClient client = new DefaultHttpClient(); HttpResponse httpResponse; try { httpResponse = client.execute(request); responseCode = httpResponse.getStatusLine().getStatusCode(); message = httpResponse.getStatusLine().getReasonPhrase(); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); response = convertStreamToString(instream); // Closing the input stream will trigger connection release instream.close(); } } catch (ClientProtocolException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } catch (IOException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** * Private method. Convert an HTTP stream to a string stream. * @param is InputStream to be converted. * @return String to be used. */ private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } } 

Then in your Android application you can create an instance of the RestClient class like this (you can even add a POST or GET parameter to the request):

 RestClient c = new RestClient("http://www.myserver.com/json.php"); c.AddParam("my_field_value", 1); try { c.Execute(RequestMethod.POST); // Here you can parse the JSON with an instance of Gson class. Gson jsonObject = new Gson(); String jsonOutput = jsonObject.toJson(c.getResponse()); // Do whatever you need with the data. } catch (Exception e) { e.printStackTrace(); } 

And voila! You can use JSON data created using a PHP script from an Android application. Remember to add android.permission.INTERNET to your AndroidManifest.xml. Otherwise, your application will not be able to contact your PHP script.

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

Good luck.

+6
source

Does this tutorial help? Connect to MySQL database

+3
source

I did this recently by calling the .Net web service to get my information. I called the web service using KSoap2 from my Android app. When searching around, I could not find any frameworks to make this easier. In the end, it turned out to be pretty easy. Here is your starting point:

http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data

+1
source

Source: https://habr.com/ru/post/1386181/


All Articles