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);
这给了我一个例外:
05-07 14:56:19.105: D/exception(1137): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
我搜索了这个,每个人都说将127.0.0.1
更改为10.0.2.2
,但我没有使用127.0.0.1
我想问题出在这一步:
String response = SignUpActivity.httpclient.execute(httppost, responseHandler);
根据定义,Localhost是指托管系统 - Android设备本身。 除非您尝试联系在Android设备上运行的服务器,否则它对您不起作用。
遵循那些建议您将 127.0.0.1(这是 localhost 的名称)更改为托管模拟器的计算机的转发别名的人的建议。
但请注意,这仅适用于模拟器,不适用于真实设备。
要做的两件事。
使用 1 http://10.0.2.2:8080/
代替您的 IP 地址
关闭eclipse
,avd
并重新启动它们。