Search code examples
androidhttpconnection

Local Host Refused


ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


                        nameValuePairs.add(new BasicNameValuePair(
                                "product_name", product_name));
                        nameValuePairs.add(new BasicNameValuePair("brand",
                                brand_product));
                        nameValuePairs.add(new BasicNameValuePair("reference_price",
                                mrp_product));
                        nameValuePairs.add(new BasicNameValuePair("model",
                                model_product));

                        HttpPost httppost = new HttpPost(
                                "http://10.0.2.2/wic3/wic2/product/doadd");

                        httppost.setEntity(new UrlEncodedFormEntity(
                                nameValuePairs));
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        String response = SignUpActivity.httpclient.execute(
                                httppost, responseHandler);
                        Log.d("response", response);

                        Intent intent = new Intent(ShareProductActivity.this, ShareActivity.class);
                        startActivity(intent);

this gives me an exception:

05-07 14:56:19.105: D/exception(1137): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused

i searched for this and everyone says change 127.0.0.1 to 10.0.2.2, but i am not using 127.0.0.1 I guess the problem is at this step:

String response = SignUpActivity.httpclient.execute(httppost, responseHandler);

Solution

  • Localhost by definition refers to the hosting system - the android device itself. Unless you are trying to contact a server running on the android device, it will not work for you.

    Follow the advice of those who suggested that you change the 127.0.0.1 (which is what localhost is a name for) to the forwarding alias for the machine hosting the emulator.

    Note however, that this only works with the emulator and not with real devices.