Java 无法从我的android emulator连接到本地web服务
我在从模拟器连接到本地web服务器时遇到问题。我可以在模拟器外部连接到它。 Emulator获取:Java 无法从我的android emulator连接到本地web服务,java,android,networking,Java,Android,Networking,我在从模拟器连接到本地web服务器时遇到问题。我可以在模拟器外部连接到它。 Emulator获取: org.apache.http.conn.httphostconnectexception连接到http//localhost;808被拒绝 protected String doInBackground(String... args) { try { // Building Parameters List<NameValueP
org.apache.http.conn.httphostconnectexception连接到http//localhost;808被拒绝
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products,
"GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println("Got an IOException: " + e.getMessage());
// TODO: handle exception
}
return null;
}
受保护的字符串doInBackground(字符串…args){
试一试{
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jParser.makeHttpRequest(url\u所有产品,
“得到”,params);
//检查日志cat中的JSON响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//发现的产品
//获取一系列产品
products=json.getJSONArray(TAG_products);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(标签PID,id);
地图放置(标签名称、名称);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}否则{
//没有发现任何产品
//启动添加新产品活动
意图i=新意图(getApplicationContext(),
NewProductActivity.class);
//关闭以前的所有活动
i、 添加标志(意图、标志、活动、清除、顶部);
星触觉(i);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}捕获(例外e){
System.out.println(“获得IOException:+e.getMessage());
//TODO:处理异常
}
返回null;
}
您可能使用了错误的网络地址来访问web服务
http://localhost
进入Android模拟器中的设备。使用10.0.2.2
访问主机环回接口(即开发机器上的127.0.0.1)
看
编辑:您的异常显示为
http//localhost;808
,但您的主机名应该是http//localhost:808
,带有:
而不是代码>尝试连接到http://10.0.2.2:8080
10.0.2.2
IP地址在模拟器中是特殊的,它将透明地连接到主机开发计算机的本地主机(127.0.0.1
)
如果在仿真器中使用localhost
或127.0.0.1
,则将改为使用内部模拟环回接口。如果仿真器中运行另一个程序,充当同一端口上的TCP或UDP服务器,这将非常有用。然而,在您的特定情况下,您确实希望“走出沙箱”
有更多信息(和示例)