简体   繁体   English

如何将计算机中的MySql数据库连接到android应用程序?

[英]how can i connect the MySql database in my computer to android application.?

how can I connect the MySQL database in my local computer to android application.? 如何将本地计算机中的MySQL数据库连接到android应用程序? is this possible .? 这可能吗 。?

I developed an android application its working perfectly. 我开发了一个完美运行的android应用程序。 Now I need access some data from MySQL database in my Local computer .I am searching a solution for this since 2 weeks. 现在我需要从本地计算机上的 MySQL数据库访问一些数据。自2周以来,我一直在寻找解决方案。 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 . 这是我本地PC中的数据库表“ Bill”。我需要从安装在移动设备中的android应用程序访问此数据库。当我在移动应用程序中输入订单ID时,我需要获取特定Oreder_id的total_amt。但是问题是表在本地计算机中 i can't access this database direclty from mobile app. 我无法从移动应用程序访问此数据库目录。 is there is any solution for do this 有什么解决办法吗

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. 您将需要在本地计算机上安装一个Web服务器,例如ApacheNGINX ,并需要一个适当的配置,以便您的计算机可以在本地网络上识别。 You should then be able to program the host machine (ie, your local computer) into your Android application for testing, and then access it over your local network. 然后,您应该能够将主机(即本地计算机)编程到Android应用程序中进行测试,然后通过本地网络访问它。 Given MySQL and webserver configuration settings, you should be able to specify a port that provides access to your local mysql server. 给定MySQL和Web服务器的配置设置,您应该能够指定用于访问本地mysql服务器的端口。

You need a server. 您需要一台服务器。 Apache servers are fine. Apache服务器很好。 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. 然后,在Android Studio中,您可以使用Webview并加载服务器。 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. 如果您打算使用每天连接的应用程序,最好看看Todd的答案 ,但是如果您只需要一些数据,则可以使用Webviews。

Here is the code: 这是代码:

MainActivity.java 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 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!!! 阿迪奥斯!

您需要在计算机上设置本地服务器,我目前正在使用Xampp来访问通过Sql数据库与我联系的Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Android应用程序连接到MySQL数据库? - How can i connect to my MySQL database with an android app? 将android应用程序连接到计算机上的数据库 - Connect android application to database on computer 如何使用JavaEE eclipse将我的React Native应用程序连接到MySQL数据库? - How can I connect my React Native application to MySQL database using JavaEE eclipse? 重新启动计算机后,Spring无法连接到mysql数据库,我如何诊断出什么问题 - Spring cant connect to mysql database after computer restarted how can i diagnose what is wrong 如何将我的Android应用程序连接到远程MySQL数据库? - How do I connect my Android app to a remote MySQL database? 如何将我的数据库连接到我的 android 应用程序 - How can i connect my database to my android app 如何将Android应用程序连接到MySQL数据库? - How to connect an Android application to a MySQL database? 如何使用jsp将android应用程序连接到mysql? - how can i connect android application to mysql using jsp? 如何使用android(PHP)连接到MySQL数据库? - How can I connect to MySQL database with android (PHP)? 连接“computer-database-jpa”使用MySQL播放2.1示例应用程序 - Connect “computer-database-jpa” Play 2.1 sample application with MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM