How do I configure my Android emulator to access my localhost?

I am trying to change my emulator network address to 10.0.2.2 so that I can test it with PHP stored on my localhost as 127.0.0.1 is an emulation native loopback interface and not the one running on my host development machine.

My system knowledge is limited, so how much of the stack do I need to tweak to achieve exactly this?

Do I need to change the default IP address of my localhost on my host machine?

Do I need to tweak something in the emulator setup?

Is there something I need to change in my connection code below?

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

    }

}

      

Change destination in my php file below?

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
?>

      

I asked this question before and was told "Just use 10.0.2.2 to connect to the localhost on your machine." and "Use your code to connect to the address." but I don't know exactly what that means.

+3


source to share


1 answer


you should change your android client code, not your PHP script. just change the following line:

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

      



to

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

      

+3


source







All Articles