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");
}