chang87812 2016-02-25 09:37 采纳率: 100%
浏览 3595
已采纳

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里面的一个线程里面了

  • 写回答

2条回答 默认 最新

  • chang87812 2016-02-25 09:42
    关注

    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");
    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名