无法从Android上我的本地服务器加载图像
我试图从服务器加载图像来显示它在ImageView的
I am trying to load an image from a server to show it in an ImageView
我用
ImageView imgView = (ImageView) findViewById(R.id.ivProduct);
Bitmap bitmap = null;
try {
URL urlImage = new URL(
"http://www.google.fr/intl/en_com/images/srpr/logo1w.png");
HttpURLConnection connection = (HttpURLConnection) urlImage
.openConnection();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
imgView.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}`
这工作得很好,但是当我下载了相同的图像我的服务器上,我改变了网址
This worked fine but when I downloaded the same image on my server and I changed the url to
http://localhost:9527/market_helper/img_products/logo1w.png
它没有工作。
有什么问题?
It did not work. What is the problem ?
的问题是,在您的网址中的http://本地主机:9527说,这是你的本地计算机上的服务器上运行,但访问时在Android中的http:// localhost的,指的是设备本身
The problem is that in your url the "http://localhost:9527" says it is running on a server on your local machine, but when accessing from your Android the "http://localhost" refers to the device itself.
如果你是在同一个网络上,你可以尝试用你的电脑的本地IP地址替换localhost的部分(例如192.168.100.6)访问它,你可以找到你的IP是由下键入ipconfig,在命令行。
If you are on the same network you can try access it by replacing the "localhost" part with your PC's local ip address (for example 192.168.100.6) You can find out what your IP is by typing "ipconfig" in the commandline.