2016-07-26 49 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

相关问题