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.


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){

    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();

        //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")+"&"
        //Writes the data to the output and ships it over the http url connection

        //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;
        return result;

    } catch (MalformedURLException e) {
    } catch (IOException e) {
    return "push method or php error";
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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