2016-07-26 46 views
0

我越來越入門例外在線程 「主」 了java.lang.RuntimeException:失敗:HTTP錯誤代碼:401

異常在線程 「主」 了java.lang.RuntimeException:失敗:HTTP錯誤代碼: 401在jasonreader.main(jasonreader.java:21)

在執行下面的代碼時,我試圖從其餘的API獲取數據。

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class jasonreader { 

    // http://localhost:8080/RESTfulExample/json/product/get 
    public static void main(String[] args) { 

     try { 

     URL url = new URL(" https://api.linkedin.com/v1/companies/1337/updates?start=20&count=10&format=json"); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Accept", "application/json"); 

     if (conn.getResponseCode() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + conn.getResponseCode()); 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream()))); 

     String output; 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
     } 

     conn.disconnect(); 

     } catch (MalformedURLException e) { 

     e.printStackTrace(); 

     } catch (IOException e) { 

     e.printStackTrace(); 

     } 

    } 

} 
+2

401表示您無權訪問該網頁。起初,您必須授權您自己。順便說一句, – TMichelsen

+0

。 JSON是一種數據格式。傑森是某人的名字。 –

+0

@ cricket_007,哈哈............ :) –

回答

0

這樣做與郵差這個要求,我這身體:

{ 
"errorCode": 0, 
"message": "Unknown authentication scheme", 
"requestId": "JP2QSQXZTG", 
"status": 401, 
"timestamp": 1469513993009 
} 

這意味着,你需要訪問令牌。請參閱this link for more infoLinkedin documentation

相關問題