Kevin Murvie
Kevin Murvie

Reputation: 2642

Android - Getting information from online database

I have created online database consisting of a table with four rows, KEY_ID, KEY_SLOT_NAME, KEY_AVAILABLE_SLOT, and KEY_TOTAL_SLOT.

Each of them has their own rows and assigned a default value and only KEY_AVAILABLE_SLOT will have its values changed.

I've created the JSONFunctions class

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

As from How to connect Android app to MySQL database?

Now I'm stuck with fourth step which is to read the JSON Tag because I don't understand what that is.

Here's a sample code usage back when I was using my offline db for testing :

final TextView parkAvaliableA = (TextView) findViewById(R.id.parkASlot);
        parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
        Button buttonA = (Button) findViewById(R.id.parkABtn);
        buttonA.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(SlotA.available_slot == 0)
                {
                    Toast.makeText(getApplicationContext(), 
                            "Parking slot A is full", 
                            Toast.LENGTH_SHORT).show();
                }
                else
                {
                    SlotA.available_slot -= 1;
                    db.updateSlot(new ParkingSlot());
                }
                //parkAvaliableA.setText("Available Slot : " + SlotA.available_slot);
                Intent myIntent = new Intent(MainMap.this, ParkA.class);

                MainMap.this.startActivity(myIntent);
            }

        });

I need basic update operation for my database so that I can update the KEY_SLOT_AVAILABLE value every button press..

And one OOT question if I may : "Should I write KEY_ in every db column? Or it's just a hassle? I started using KEY_ when I read some online tutorials use KEY_"

Upvotes: 0

Views: 79

Answers (1)

keshav kowshik
keshav kowshik

Reputation: 2374

You need to write a script as shown below to connect to a MySql Database:

 <?php
    $servername = "Your_Server_Name";
    $username = "Your_UserName";
    $password = "Your_Password";
    $dbname = "Your_DB_Name";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);



   // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

After successful connection to the database you can perform all db operations with the server.

Check the following links you will get an idea:

http://www.w3schools.com/php/php_mysql_create.asp

http://www.tutorialspoint.com/php/create_mysql_database_using_php.htm

Hope this helps.

Upvotes: 1

Related Questions