程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 線程-android 訪問網絡 拒絕連接

線程-android 訪問網絡 拒絕連接

編輯:編程解疑
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");

    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved