來源

2012-12-28 28 views
0

我是新來的黑莓應用程序的開發。我有一個愚蠢的錯誤,而在HttpConnectionhttpcon.getResponseCode()方法給源找不到錯誤。來源

請,任何一個可以弄清楚這個錯誤?

這裏是我的方法:

 net.rim.device.api.io.transport.ConnectionFactory cf = new net.rim.device.api.io.transport.ConnectionFactory(); 
     httpConn = (HttpConnection) cf.getConnection(url).getConnection(); 

     httpConn.setRequestMethod(HttpConnection.POST); 
     httpConn.setRequestProperty("User-Agent", 
       "Profile/MIDP-2.0 Confirguration/CLDC-1.0"); 
     httpConn.setRequestProperty("Accept_Language", "en-US"); 
     httpConn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 

     httpConn.setRequestProperty("Content-Length", 
       Integer.toString(postData.length)); 
     os = httpConn.openOutputStream(); 
     os.write(("[email protected]&Password=yah123") 
       .getBytes("UTF-8")); 
     os.flush(); 
     os.close(); 
     try { 
      responseCode = httpConn.getResponseCode(); 
     } catch (IOException ex1) { 
      //check if it's eof, if yes retrieve code again 
      if (-1 != ex1.getMessage().indexOf("EOF")) { 
       try { 
        responseCode = httpConn.getResponseCode(); 
       } catch (IOException ex2) { 
        System.out.println(ex2.getMessage()); 
        // handle exception 
       } 
      } else { 
       System.out.println(ex1.getMessage()); 
       // handle exception 
      } 
     } 
     int status = httpConn.getResponseCode(); 

     if (status == HttpConnection.HTTP_OK) { 

      InputStream input = httpConn.openInputStream(); 

      byte[] bytes = IOUtilities.streamToBytes(input); 

      StringBuffer raw = new StringBuffer(new String(bytes)); 
      raw.insert(0, "bytes received]\n"); 
      raw.insert(0, bytes.length); 
      raw.insert(0, '['); 

      url = raw.toString(); 

      input.close(); 
     } else { 
      url = "response code = " + status; 
     } 
     httpConn.close(); 
    } catch (IOCancelledException e) { 
     System.out.println(e.toString()); 
     return ""; 
    } catch (IOException e) { 

     return ""; 
    } 

    return ""; 
} 

更新:我不想踏進getResponseCode()。 Eclipse正在停止執行,並顯示Source Not Found錯誤。

+0

什麼是你想怎麼辦?這是一個**編譯**錯誤,或者你是否試圖在調試器中進入一個方法? – Nate

+0

@Nate是的,我們是一步進入方法當時只有我們正在運行時間Error.Error顯示當'Getresponsecode()被調用'。 – RajeshKdev

+0

@Nate我發現我的錯誤。當調用'HttpConnection。 getResponseCode()'在那個URL字符串中我們必須添加'deviceside = true',那麼只會調用'getresponsecode()'而不會拋出任何http異常。例如:'httpClient ht = new httpClient(); 字符串str = ht.getHttpClientResponse( 「HTTPS://www.google.co.in;裝置側=真」,後);' – RajeshKdev

回答

1

我發現我的mistake.When在URL字符串,我們必須添加裝置側= true,則只能getresponsecode調用HttpConnection. getResponseCode()()將被調用沒有拋出任何HTTP例外。

例如: httpClient ht = new httpClient(); String str = ht.getHttpClientResponse("https://www.google.co.in;deviceside=true",post);

注: (從@Nate評論) 我們不要總想在你的連接字符串deviceside=true。這取決於您是在設備上還是在模擬器上運行,以及可用的網絡類型。

2

@Nate是的,我們是一步進入方法當時只有我們正在運行時間Error.Error顯示時Getresponsecode()被調用

貴日食錯誤這個樣子的?

enter image description here

getResponseCode()處於HttpConnection類的方法。這是RIM的代碼,不是你的。您通常不需要將代碼步驟轉換爲。在調試時,只需執行而不是那行。你想看到的唯一的事情就是該方法的結果,裏面沒有發生什麼變化。

即使您的項目中包含net_rim_api.jar,也只是簡單地給出了RIM類的二進制版本,包括HttpConnection。它不提供該類的Java代碼。爲了將步驟轉換爲的一種方法,您也需要擁有源代碼。

+0

是它的'Data gram Protocol(連接基礎).receive(data gram)line 538'在標題中。 – RajeshKdev

+0

我不確定我是否完全理解您的評論。 Eclipse是否告訴你它無法找到DatagramProtocol的來源?當你嘗試**進入** getResponseCode()方法,或者程序停止在該行執行時,它是這樣做的嗎?確保你明白我在這裏問的問題(如果沒有,請告訴我)。如果調試器自行停止,則應用程序拋出異常。 Eclipse可能設置爲自動處理未處理的異常。但是,我需要知道,如果您實際上是**嘗試**來**進入** RIM庫代碼。 – Nate

+0

也可以多次調用'getResponseCode()'。你能告訴我們哪個調用getResponseCode()給你提供這些問題嗎?謝謝。 – Nate