0

how can I connect the MySQL database in my local computer to android application.? is this possible .?

I developed an android application its working perfectly. Now I need access some data from MySQL database in my Local computer .I am searching a solution for this since 2 weeks. kindly help me to solve

---------------------------
| Order_id  |   Toatl Amt |
|-------------------------|
| 1025566   | 99.50       |
|-------------------------|
| 1125426   | 50.00       |
|-------------------------|
| 1025555   | 150.00      |
---------------------------

this is my data base table "Bill" in my local PC .i need access this database from android application installed in my mobile .when i enter the order id in my mobile app i need get total_amt of specific Oreder_id.but the problem is this table is in Local Computer. i can't access this database direclty from mobile app. is there is any solution for do this

Dhanish
  • 5
  • 7
  • Possible duplicate of [Can we connect remote MySQL database in Android using JDBC?](http://stackoverflow.com/questions/26470117/can-we-connect-remote-mysql-database-in-android-using-jdbc) – SpringLearner Dec 01 '15 at 11:48
  • can i access data from database in my PC.? if i develop a rest service in my PC. – Dhanish Dec 01 '15 at 11:48

3 Answers3

0

You would need a webserver on your local machine, like Apache or NGINX, and an appropriate configuration such that your machine is recognized on your local network. You should then be able to program the host machine (i.e., your local computer) into your Android application for testing, and then access it over your local network. Given MySQL and webserver configuration settings, you should be able to specify a port that provides access to your local mysql server.

Todd
  • 2,824
  • 2
  • 29
  • 39
  • Thank you for your reaply i have database in my PC name is "loyalty" .i have one table "Bill" under this database have two feilds. order_id and Total_amt. my problem is. i need access this database from android application installed in my mobile .when i enter the order id in my mobile app i need get total_amt of specific order_id. – Dhanish Dec 01 '15 at 19:42
0

You need a server. Apache servers are fine. The easiest way (and a little bit dirty) is to make a login in your server for whatever the App is about. Then in Android Studio you can use a Webview and load your server. You need to know that it usually takes time to load. If you are planning to do an app with an everyday connection, is better to look at Todd's answer, but if you only need some data, you will be OK working with Webviews.

Here is the code:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    WebView mWebView; //your webview
    final String url ="http://www.yourwebpage.com"; // string with your webpage url
    ProgressDialog mProgress; //progress dialog that appears if your connection is not good (its an extra)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        getSupportActionBar().hide();
        mWebView = (WebView) findViewById(R.id.webView); //your webview
        WebSettings ajustes = mWebView.getSettings(); //your websettings (i am spanish so i called it "ajustes")
        ajustes.setJavaScriptEnabled(true); //enable javascript in your webview
        ajustes.setBuiltInZoomControls(true);
        ajustes.setSupportZoom(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);

        mWebView.setWebViewClient(new MyWebClient());
        mWebView.loadUrl(url); // here you call your url string that you made before
        mProgress = ProgressDialog.show(this, "Loading...", "Wait please...."); // progress dialog text

        mWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) { // in this fuction you deal with connection error so you show a toast that tells you that tere is not connection

                Toast.makeText(main_activity.this, "THERE IS NOT CONNECTION.TRY AGAIN LATER", Toast.LENGTH_LONG).show();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) { //when your page finishes loading, your progressdialog will disappear
                if (mProgress.isShowing()) {
                    mProgress.dismiss();
                }
            }
        });
        // set url for webview to load
        mWebView.loadUrl(url);
    }

    private class MyWebClient extends WebViewClient {
        //AlertDialog.Builder alert = new AlertDialog.Builder(web.this);
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

So that was all the code. If you need more aske me. Adiós!!!

byDavid360
  • 11
  • 5
  • Thank you @byDavid360. i have database in my PC name is "loyalty" .i have one table "Bill" under this database have two feilds. order_id and Total_amt. my problem is. i need access this database from android application installed in my mobile .when i enter the order id in my mobile app i need get total_amt of specific order_id. – Dhanish Dec 01 '15 at 19:46
  • @Dhanish So a webview is not ok for you? – byDavid360 Dec 01 '15 at 20:15
  • If you find a better way, tell me please. I am also an android programmer and i couldnt connect my server apache with android app yet without a webview. Thanks!!! – byDavid360 Dec 01 '15 at 20:16
0

You need to setup a localserver on your computer ,, i currently using Xampp to access my web service that contact me with my Sql DataBase

S. Alawadi
  • 144
  • 1
  • 6
  • Thank you for your reaply i have database in my PC name is "loyalty" .i have one table "Bill" under this database have two feilds. order_id and Total_amt. my problem is. i need access this database from android application installed in my mobile .when i enter the order id in my mobile app i need get total_amt of specific order_id. – Dhanish Dec 01 '15 at 19:43
  • ok , you need to make connection via HTTPClinet or HTTPURLConection to access the webservice that get the value you nedded – S. Alawadi Dec 02 '15 at 08:02