简体   繁体   中英

Is It Possible to test Android Application On Mobile While Connected to mysql

Sorry I am newbie in and I don't have VT Technology to install emulator on my pc so I wanted to ask that is it possible to test android application on mobile phone while your android application is connected to localhost mysql server which is in your pc.

I am trying but my application is not connecting to the mysql

if this is possible than my code is

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

this is the code of my backgroundWorker class where I have used the async task

 protected String doInBackground(String... params) {
        String method = params[0];
        String weburl = "http://192.168.1.103/And/receive.php"; // this is the  localhost pc address
        if (method.equals("login")) {
            String username = params[1];
            String password = params[2];

            try {

                URL jv = new URL(weburl);
                HttpURLConnection httpURLConnection = (HttpURLConnection) jv.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
                        + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                bf.write(data);

                bf.flush();
                bf.close();
                OS.close();
                InputStream is = httpURLConnection.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));

                String result = "";
                String line = "";
                while ((line = br.readLine()) != null) {
                    result += line;
                }
                bf.close();
                is.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return null;
    }

this is the code of my android textfield which is inside the main Activity

public void Login(View v){

    String username=usernameEr.getText().toString();
    String password=passwordEr.getText().toString();

    String type="login";

    BackgroundWorker BgWorker= new BackgroundWorker(this);
    BgWorker.execute(type,username,password);

}

this is my php code

<?php

include "connection.php";

$fname=$_POST['user'];
$password=$_POST['password'];


$row=mysql_query("SELECT * FROM tayyab where username='$fname' AND password='$password'") or die("query failed");

$row_count=mysql_num_rows($row);

if($row_count>=1){

    echo "You have been logged in!";

}
else{

        echo "You have been logged out!";

}

?>

In your URL you have not included port address. For instance

String url= "http://192.168.0.1:8084/YourApplication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM