简体   繁体   中英

ANDROID - Can't connect database from localhost in Android Device

My code is working in Android Emulator. I can connect to database from localhost...

But when I run in my Android Device. The app isn't run well..

Can you give reference for connect database from localhost to Android Device ? I have searched in Google, but I don't get it.

    public class Login extends Activity implements View.OnTouchListener {

    EditText usernameInput, passwordInput;
    Button loginButton;
    String username, password;
    ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();
    TextView createAccountLink;
    private static String login_url = "http://10.0.2.2/ujian_online/login.php";
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    ArrayList<HashMap<String, String>> arraylist;
    JSONArray products = null;
    String image_link;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        usernameInput = (EditText) findViewById(R.id.username);
        passwordInput = (EditText) findViewById(R.id.password);
        loginButton = (Button) findViewById(R.id.login_button);
        jsonParser = new JSONParser();
        createAccountLink = (TextView) findViewById(R.id.createAccountLabel);
        createAccountLink.setOnTouchListener(this);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new CheckAccounts().execute();
            }
        });
        //HANCURIN SESSION
    }

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
      if(v.getId() == R.id.createAccountLabel) {
          Intent i = new Intent(getApplicationContext(), Registration.class);
          startActivity(i);
          finish();
      }
      return true; 
    }

    public class CheckAccounts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("System checks your account.....");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            username = usernameInput.getText().toString();
            password = passwordInput.getText().toString();
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", username));
            params.add(new BasicNameValuePair("password", password));
            JSONObject json = jsonParser.makeHttpRequest(login_url, "POST", params);

            try {
                int success = json.getInt("success");
                Log.d("SUKSES", String.valueOf(success));
                int id = 0;
                if (success == 1) {
                    products = json.getJSONArray(TAG_PRODUCTS);
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);
                         id = c.getInt("id");
                    }   
                    new SessionManagement(getApplicationContext()).putInteger("ID", id);
                    Intent i = new Intent(getApplicationContext(), MainMenu.class);
                    startActivity(i);
                    finish();
                } else {
                    pDialog.setMessage("Your username or password is wrong");
                    pDialog.show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }

    }
}

Your are using 10.0.2.2 which is a special url for emulator ONLY to point to the machine localhost. For real devices use actual ip address of the machine and be on same network.

The url you are using should only be used in emulators.

Use it like this

private static String login_url = "http://real_ip_of_your_machine/ujian_online/login.php";

Get the ip of your maching using ipconfig(Windows) or ifconfig (Linux/Mac). Also, your machine and the device should be on same network.

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