【问题标题】:How do I configure my Android emulator to access my localhost?如何配置我的 Android 模拟器以访问我的本地主机?
【发布时间】:2015-09-01 20:11:06
【问题描述】:

我正在尝试将我的模拟器的网络地址更改为 10.0.2.2,以便我可以使用存储在我的本地主机上的 PHP 对其进行测试,因为 127.0.0.1 是模拟系统自己的环回接口,而不是在我的主机开发机器上运行的那个.

我的系统知识有限,那么我需要配置堆栈的哪一部分才能准确实现这一点?

是否需要更改主机上本地主机的默认 IP?

我需要在模拟器设置中进行一些配置吗?

我需要在下面的连接代码中更改什么吗?

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

    String user;
    int success;
    Context context;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PREDICTION = "prediction";
    private static final String TAG_OCCUPANCY_PREDICTION = "occupancy_prediction";

    private static final String url_prediction = "http://localhost/android_connect/get_prediction.php";

    /**
     * Getting product details in background thread
     * */

    public UpdatePrediction(Context context) {
        this.context = context;
    }

    protected String doInBackground(String... param) {

        SharedPreferences sharedPref = context.getSharedPreferences("user",
                Context.MODE_PRIVATE);
        user = sharedPref.getString("name", "codohert");
        Log.i("update", "started");

        try {

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("pid", user));

            // getting product details by making HTTP request
            // Note that product details url will use GET request
            JSONObject json = jsonParser.makeHttpRequest(url_prediction, "GET",
                    params);

            // check your log for json response
            Log.d("Prediction Details", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.i("update", "success");
                // successfully received product details
                JSONArray predictionObj = json.getJSONArray(TAG_PREDICTION); // JSON
                                                                                // Array

                // get first product object from JSON Array
                JSONObject prediction = predictionObj.getJSONObject(0);

                String predict = prediction.getString(TAG_OCCUPANCY_PREDICTION);
                String shour = predict.substring(11, 12);
                String smin = predict.substring(14, 15);
                Integer hour = Integer.parseInt(shour);
                Integer minute = Integer.parseInt(smin);

                SetTime(hour, minute);
                return null;

            } else {
                Log.i("update", "failed");
                return null;
            }
        } catch (JSONException e) {
            Log.i("update", "catch");
            e.printStackTrace();
            return null;
        }
    }

    public void SetTime(int hourOfDay, int minute) {
        SharedPreferences sharedPref = context.getSharedPreferences(
                "prediction", Context.MODE_PRIVATE);
        SharedPreferences.Editor editorh = sharedPref.edit();
        editorh.putInt("prediction_hour", hourOfDay);
        editorh.commit();
        SharedPreferences.Editor editorm = sharedPref.edit();
        editorm.putInt("prediction_min", minute);
        editorm.commit();
        Toast.makeText(context, "Set Prediction", Toast.LENGTH_SHORT).show();

    }

}

我是否在下面的 php 文件中更改目标?

db_config.php
<?php

/*
 * All database connection variables
 */

define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "habitabilitystudy"); // database name
define('DB_SERVER', "localhost"); // db server
?>

我之前问过这个问题,并被告知“只需使用 10.0.2.2 连接到计算机上的 localhost。”和“使用您的代码连接到该地址。”但我不知道具体指的是什么。

【问题讨论】:

  • 你必须改变你的安卓代码。使用 10.0.2.2 而不是 localhost

标签: php android android-emulator webserver localhost


【解决方案1】:

您应该更改您的 android 客户端代码,而不是您的 php 脚本。 只需更改以下行:

`private static final String url_prediction = "http://localhost/android_connect/get_prediction.php";`

 `private static final String url_prediction = "http://10.0.2.2/android_connect/get_prediction.php";`

【讨论】:

  • 我现在收到一条错误消息:org.apache.http.conn.HttpHostConnectException: Connection to 10.0.2.2 denied
  • 检查你的manifest.xml,你应该添加互联网权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-13
  • 2011-07-01
  • 2020-08-28
  • 2012-10-20
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多