2013-02-16 42 views
0

我有一個應用程序,我通過POST方法發送短信,這種方法工作正常,但有一段時間給予Fi​​leNotFound異常。請任何人告訴我最新的問題。如何解決FileNotFoundException incase HttpURLConnection

以下是我的代碼。由於

public static String sendSMS(String data, String url1) { 
      URL url; 

      String status = "Somthing wrong "; 
      try { 
       url = new URL(url1); 
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();   
       connection.setDoOutput(true); 
       connection.setDoInput(true); 
       connection.setInstanceFollowRedirects(false); 
       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestProperty("charset", "utf-8"); 
       connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); 
       connection.setUseCaches (false); 

       DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
       wr.writeBytes(data); 
       wr.flush(); 
       wr.close(); 
       connection.disconnect(); 

       // Get the response 
       try { 
        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
        String s; 
        while ((s = rd.readLine()) != null) { 
         status = s; 
        } 
        rd.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 



      } catch (MalformedURLException e) { 
       status = "MalformedURLException Exception in sendSMS"; 
       e.printStackTrace(); 
      } catch (IOException e) { 
       status = "IO Exception in sendSMS"; 
       e.printStackTrace(); 
      } 

      return status; 
     } 

我有什麼錯誤

java.io.FileNotFoundException:HTTP:// ......在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection類.java:1401)

回答

3

您在讀取輸入流之前斷開連接。移動此行代碼:

connection.disconnect(); 

於某點您已經閱讀響應流後。

+0

Hi Perception,我在響應流之後移動了代碼,但是之後我得到了FileNotFoundException你能告訴我爲什麼會發生這種情況....任何想法?...這個異常發生在100箇中有10個 – Narendra 2013-02-18 12:12:26

+0

@Narendra - 你可能要發佈一個單獨的問題,並提供足夠的細節(當前代碼示例,異常堆棧跟蹤)。 – Perception 2013-02-18 13:24:53

+0

@Narendra - 爲什麼不接受? – Perception 2013-02-21 11:35:20

相關問題