2015-10-17 38 views
1

林試圖從這個API返回一個JSON字符串: https://market.mashape.com/pareshchouhan/trivia從URL加載API時發生FileNotFoundException。 Java的。 Android的

但是,Java拋出錯誤:

java.io.FileNotFoundException: https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions?limit=10&page=1 

上線

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

我已經使用了類似的代碼其他Rest API,所以我有點不確定這是爲什麼發生。

static String jsonStr; 
    String data = ""; 


    @Override 
    protected Void doInBackground(Void... params) { 
     BufferedReader br = null; 


     try { 

      URL url; 
      String urlStr = "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions?limit=10&page=1"; 
      url = new URL(urlStr); 

      URLConnection connection = url.openConnection(); 
      connection.setRequestProperty("X-Mashape-Key", "4OFryNEYTWmshe8GheSnmIVEj7gZp1kJf6cjsncjVhXj9WYACn"); 
      connection.setRequestProperty("Accept", "application/json"); 
      connection.setDoOutput(true); 

      OutputStreamWriter outputStreamWr = new OutputStreamWriter(connection.getOutputStream()); 
      outputStreamWr.write(data); 
      outputStreamWr.flush(); 



      br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while((line = br.readLine())!=null) { 
       sb.append(line); 
       sb.append(System.getProperty("line.separator")); 
      } 

      jsonStr = sb.toString(); 


     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 

    } 

任何幫助將不勝感激。 此致敬禮。

回答

相關問題