【问题标题】:ANDROID - Can't connect database from localhost in Android DeviceANDROID - 无法从 Android 设备中的本地主机连接数据库
【发布时间】:2015-03-05 13:31:34
【问题描述】:

我的代码在 Android 模拟器中运行。我可以从本地主机连接到数据库...

但是当我在我的 Android 设备上运行时。该应用程序运行不佳..

您能否提供 reference 以将数据库从 localhost 连接到 Android 设备?我在谷歌上搜索过,但我不明白。

    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();
        }

    }
}

【问题讨论】:

  • 您的测试设备是否在同一个网络中?当您的桌面在本地网络中时,您的设备需要通过 wifi 在同一个网络中。否则,您的数据库需要可通过 Internet 访问,并且您的设备需要 Internet 连接。
  • 你正在使用10.0.2.2 这是一个特殊的 url 模拟器只指向机器本地主机。对于真实设备,请使用机器的实际 IP 地址并在同一网络上。

标签: android json localhost device


【解决方案1】:

您正在使用 10.0.2.2,这是一个特殊的 url,仅用于模拟器指向机器 localhost。对于真实设备,请使用机器的实际 IP 地址并在同一网络上。

您使用的网址只能在模拟器中使用。

这样使用

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

使用 ipconfig(Windows) 或 ifconfig (Linux/Mac) 获取您的机器的 ip。此外,您的机器和设备应该在同一网络上。

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 2014-01-14
    • 1970-01-01
    • 2021-11-03
    • 2018-09-16
    相关资源
    最近更新 更多