httpURLConnection 报错 java net ConnectException localhost/12

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

                03-20 13:56:19.120 382-393/? W/System.err: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
03-20 13:56:19.170 382-393/? W/System.err:     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
03-20 13:56:19.170 382-393/? W/System.err:     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
03-20 13:56:19.180 382-393/? W/System.err:     at java.net.Socket.connect(Socket.java:983)
03-20 13:56:19.180 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
03-20 13:56:19.220 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
03-20 13:56:19.220 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
03-20 13:56:19.220 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
03-20 13:56:19.220 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
03-20 13:56:19.230 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
03-20 13:56:19.230 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
03-20 13:56:19.230 382-393/? W/System.err:     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:726)

03-20 13:56:19.250 382-393/? W/System.err:     at tech.androidstudio.readfileshttpurlconnection.MainActivity.run(MainActivity.java:38)


原因分析:

我在模拟器上面运行的时候localhost 就是模拟器的ip地址了,所以在请求的时候的URL 里面应该使用的是电脑的ip地址。


解决方法:

将下面的

URL url = new URL("http://localhost:8080/tomcat.png");
修改为

URL url = new URL("http://192.168.1.106:8080/tomcat.png");


package tech.androidstudio.readfileshttpurlconnection;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;public class MainActivity extends AppCompatActivity implements Runnable {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Thread thread = new Thread(this);        thread.start();    }    @Override    public void run() {        try {            //TODO 这里的ip 地址一定不能使localhost 一定要是电脑的或者是正式ip地址.            //如果写成了localhost,那么就会报错java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused//            URL url = new URL("http://localhost:8080/tomcat.png");            URL url = new URL("http://192.168.1.106:8080/tomcat.png");            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();            httpURLConnection.setRequestMethod("GET");            httpURLConnection.setConnectTimeout(5000);            //Sets the flag indicating whether this URLConnection allows input. It cannot be set after the connection is established.            httpURLConnection.setDoInput(true);            InputStream in = null;            FileOutputStream fos = null;            if (httpURLConnection.getResponseCode() == 200) {                in = httpURLConnection.getInputStream();                //一定不能直接在FileOutputStream里面写文件名,需要添加路径                //错误的写法:fos = new FileOutputStream("a.bmp");                //下面存储到内部存储的私有的cache目录里面,注意了生成的文件名是cachea.bmp                fos = new FileOutputStream(getCacheDir().getPath()+"a.bmp");                byte[] arr = new byte[1024];                int len = 0;                while ((len = in.read(arr)) != -1) {                    fos.write(arr, 0, len);                }                in.close();                fos.close();            }        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (ProtocolException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}



           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值