【发布时间】:2013-01-29 12:04:11
【问题描述】:
请告诉我一些,如何解决这个问题, 有时我收到 Filenotfound 异常,有时这段代码运行良好。
下面是我的代码,
public String sendSMS(String data, String url1) {
URL url;
String status = "Somthing wrong ";
try {
url = new URL(url1);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
conn.setRequestProperty("Accept","*/*");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String s;
while ((s = rd.readLine()) != null) {
status = s;
}
rd.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
wr.close();
} catch (MalformedURLException e) {
status = "MalformedURLException Exception in sendSMS";
e.printStackTrace();
} catch (IOException e) {
status = "IO Exception in sendSMS";
e.printStackTrace();
}
return status;
}
【问题讨论】:
-
你能显示堆栈跟踪和你得到的错误信息吗?
-
url1的值是多少? -
您是否在异常消息中看到“打开的文件过多”?
-
尝试编写 wr.close();紧接在 wr.flush() 之后