-2

I have a website for admin and a mobile app for normal user. Both of them share a same database which is now on the cloud(The website for admin is already deployed with the database on the server). The way I use to connect to our local database for our mobile app is through PHP scripts which contains the necessary parameters. The problem now is I do not know how to connect my mobile app(it is not deployed yet) to the database(which is now on the server). Is there any way to guide me ? Thank you in advance.

0

I assume you are using a php get or post to communicate with the server. You would have to create a url in your Android application to connect to the php script on the server. For example, assuming you have a script called push.php on the server, this would connect to login_url.com/push.phpYOURPOSTMESSAGEHERE:

//Pushes a line of data to the server
//Column reference: thing1, thing2, thing3, thing4, thing5
protected String push(String login_url, String[] params){
    login_url+="/push.php";

    try {
        //This has to be inside the try/catch
        URL url = new URL(login_url);

        //See column reference at top of method
        String col1 = params[1];
        String col2 = params[2];
        String col3=params[3];
        String col4=params[4];
        String col5=params[5];
  //      String col6=params[6]; //etc for more lines of data

        //Opens a connection to the specified URL and reserves input/output privileges
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);

        //Opens a container for the data to transmit to the server
        OutputStream outputStream = httpURLConnection.getOutputStream();
        //Sends text in UTF-8 format, should be compliant with any SQL implementation
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        //Generates a string from the data
        String post_data = URLEncoder.encode("thing1","UTF-8")+"="+URLEncoder.encode(col1,"UTF-8")+"&"
                +URLEncoder.encode("thing2","UTF-8")+"="+URLEncoder.encode(col2,"UTF-8")+"&"
                +URLEncoder.encode("thing3","UTF-8")+"="+URLEncoder.encode(col3,"UTF-8")+"&"
                +URLEncoder.encode("thing4","UTF-8")+"="+URLEncoder.encode(col4,"UTF-8")+"&"
                +URLEncoder.encode("thing5","UTF-8")+"="+URLEncoder.encode(col5,"UTF-8");
        //Writes the data to the output and ships it over the http url connection
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();

        //Opens a container to receive input from the server
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
        String result="";
        String line="";
        while((line = bufferedReader.readLine())!= null) {
            result += line;
        }
        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();
        return result;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "push method or php error";
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.