2013-12-10 51 views
4

我以XML格式從穩定的Web服務檢索數據,但不幸的是我的Stringnull。請幫助我,我的代碼中的問題在哪裏?從Java中的URL讀取XML字符串

下面是示例代碼:

URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd"); 
HttpURLConnection request1 = (HttpURLConnection) url.openConnection(); 
request1.setRequestMethod("GET"); 
request1.connect(); 
InputStream is = request1.getInputStream(); 
BufferedReader bf_reader = new BufferedReader(new InputStreamReader(is)); 
String responseData = IOUtils.toString(bf_reader); 
System.out.print(responseData); 

我已經試過的網址,其餘客戶端:這是我返回正確的XML,但在這裏我Stringnull

回答

2

您可以閱讀響應像

BufferedReader bufferedReader = 
    new BufferedReader(new InputStreamReader(request1.getInputStream())); 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      System.out.print(line); 
     } 

獲取RESP 302代碼。與

URL url = new URL("https://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd"); 
+0

其再次空 –

+0

檢查瀏覽器 – vels4j

+0

同一耶其返回給我正確的XML –

2

試試只是直接傳遞請求的InputStream IOUtils.toString(), 因爲IOUtils.toString使自身的緩衝。

此外,如果請求 已成功或存在任何類型的Http錯誤,您可以使用request1.getResponseCode()進行檢查。

此外,不需要調用connect()方法,因爲在getInputStream()調用中隱式地執行了 。

更新:也得到錯誤302.當然我d'ont有正確的登錄數據。但該請求必須使用以下代碼:

try { 
    URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail& password=mypwd"); 
    //URL url = new URL("http://www.google.de"); 
    HttpURLConnection request1 = (HttpURLConnection) url.openConnection(); 
    request1.setRequestMethod("GET"); 
    // request1.connect(); 
    String code = String.valueOf(request1.getResponseCode()); 
    System.out.println("Error code "+code); 
    InputStream is = request1.getInputStream(); 

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); 
    String line; 
    while ((line = bufferedReader.readLine()) != null) { 
    System.out.println(line); 
    } 
    // BufferedReader bf_reader = new BufferedReader(new InputStreamReader(is)); 
    // String responseData = IOUtils.toString(bf_reader); 
    // System.out.print(responseData); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
1

嘗試使用掃描儀將InputStream轉換爲字符串。我試過這段代碼,工作正常。

URL url = new URL("http://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd"); 
    HttpURLConnection request1 = (HttpURLConnection) url.openConnection(); 
    request1.setRequestMethod("GET"); 
    request1.connect(); 
    InputStream is = request1.getInputStream(); 
    String inputStreamString = new Scanner(is,"UTF-8").useDelimiter("\\A").next(); 
    System.out.print(inputStreamString); 
2

問題是302 HTTP錯誤,這意味着服務器執行重定向。

如果您將URL從HTTP更改爲HTTPS,則它會以預期的方式工作。 請求的結果是:

OK 
Error code 200 
<?xml version="1.0" encoding="UTF-8"?><response><error code="1"><![CDATA[Incorrect password or username]]></error></response> 

您可以通過評估與HTTP URL請求的響應頭得到重定向地址。您可以通過檢查密鑰「位置」的值來找到重定向網址。

String reUrl = request1.getHeaderField("Location"); 
System.out.println("Redirect URL "+reUrl); 

導致到下面的輸出:

Found 
Error code 302 
Redirect URL https://loxvo.fogbugz.com/api.asp?cmd=logon&email=myemail&password=mypwd