android 访问网络 拒绝连接

java.net.ConnectException: failed to connect to /192.168.10.98 (port 8080) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused)

第一次 打开应用的时候可以一直访问网络,只要是把Service 后台服务停了 在打开服务就报错!

点击一下住页面,就又可以访问网络,怎么解决呢?

在后台运行久了 也是不能访问网络,次数判断 网络为 true 类型为wifi
联网的代码是写在 Service里面的一个线程里面了

if (NetWorkUtil.isWifiConnected(context) && NetWorkUtil.getAPNType(context).equals("WIFI")) {

            try {


                String result = null;

                File file =  new File(f);
                if (!file.exists() || !file.isFile()) {

                    throw new IOException("文件不存在");

                }

                /**
                 * 第一部分
                 */
                URL urlObj = new URL("http://63.223.85.201:8080/updownload/res?method=upload");
                // 连接
                HttpURLConnection con =  (HttpURLConnection) urlObj.openConnection();

                /**
                 * 设置关键值
                 */
                con.setRequestMethod("POST");
                // 以Post方式提交表单,默认get方式
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setUseCaches(false); // post方式不能使用缓存

                // 设置请求头信息
                con.setRequestProperty("Connection", "Keep-Alive");
                con.setRequestProperty("Charset", "UTF-8");

                // 设置边界
                String BOUNDARY = "----------" + System.currentTimeMillis();
                con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

                // 请求正文信息

                // 第一部分:
                StringBuilder sb = new StringBuilder();
                sb.append("--"); // 必须多两道线
                sb.append(BOUNDARY);
                sb.append("\r\n");
                sb.append("Content-Disposition: form-data;name=\"res\";filename=\"" + file.getName() + "\"\r\n");
                sb.append("Content-Type:application/octet-stream\r\n\r\n");
                sb.append("Content-Type:application/octet-stream\r\n\r\n");
                String part2 = "\r\n" + "--" + BOUNDARY + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Disposition: form-data; name=\"title\"" + "\r\n" + "\r\n" + Util.getTime() +" vo"+ "\r\n" + "--" + BOUNDARY + "--";
                String part1 = "\r\n" + "--" + BOUNDARY + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Disposition: form-data; name=\"note\"" + "\r\n" + "\r\n" + "ggL" + "\r\n" + "--" + BOUNDARY + "--";

                byte[] head = null;
                head = (sb.toString()).getBytes("utf-8");

                // 获得输出流
                OutputStream out = null;
                out = new DataOutputStream(con.getOutputStream());
                // 输出表头
                out.write(((part1.toString()).getBytes("utf-8")));
                out.write(((part2.toString()).getBytes("utf-8")));
                out.write(head);

                // 文件正文部分
                // 把文件已流文件的方式 推入到url中
                DataInputStream in = null;
                in = new DataInputStream(new FileInputStream(file));
                int bytes = 0;
                byte[] bufferOut = new byte[1024];
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
                in.close();
                // 结尾部分
                byte[] foot = null;
                foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
                out.write(foot);
                out.flush();
                out.close();
                StringBuffer buffer = new StringBuffer();
                BufferedReader reader = null;
                try {
                    // 定义BufferedReader输入流来读取URL的响应
                    reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        // System.out.println(line);
                        buffer.append(line);
                    }
                    if (result == null) {
                        result = buffer.toString();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    try {
                        throw new IOException("数据读取异常");
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                    }
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                        }
                    }

                }

            //  return result;



            //  FileUtile.send(actionUrl,number,f);
                //byte[] b = u.send(actionUrl);
                //String strRead = new String(b);
                //strRead = String.copyValueOf(strRead.toCharArray(), 0, b.length);
            FileManagerUtil.writeuplaodinfo(NetWorkUtil.isNetworkConnected(context) + "   fsdf" + "\r\n");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                FileManagerUtil.writeuplaodinfo(NetWorkUtil.isNetworkConnected(context) + " upload  " + e + "\r\n");
            }

        }
        //System.gc();
    } catch (Exception e) {

        FileManagerUtil.writeuplaodinfo(Util.getTime() + " error  " + e + "\r\n");

    }

你重新关闭网络再次连接,然后关闭adb连接,重新连接上试试