2013-05-08 89 views
0

我試圖通過REST服務從shopify API獲取產品記錄。但我得到這個錯誤{"errors":{"product":"can't be blank"}}下面是代碼片段。Java Servlet如何從Shopify API獲取json響應

  String getURL = "https://myshop.myshopify.com/admin/products.json"; 

      url = new URL(getURL); 

      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.setRequestProperty("Content-Type", 
       "application-json"); 
      connection.setRequestProperty("X-Shopify-Access-Token",token); 
      connection.setUseCaches(false); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      // Send request 
      DataOutputStream w = new DataOutputStream(
        connection.getOutputStream()); 
      w.flush(); 
      w.close(); 


      // Get Response 
      InputStream i = connection.getInputStream(); 
      BufferedReader streamReader = new BufferedReader(new InputStreamReader(i, "UTF-8")); 
      StringBuilder responseStrBuilder = new StringBuilder(); 

      String inputStr; 
      while ((inputStr = streamReader.readLine()) != null){ 
       System.out.println(inputStr); 
       responseStrBuilder.append(inputStr); 
      } 
+0

錯誤信息提示你是不是通過在您需要的產品信息產品ID /密碼。看起來像期待在請求中輸入一些內容。 – sudmong 2013-05-08 11:05:20

+0

它應該是沒有必要的參數,因爲如果我直接粘貼到瀏覽器的url它可以訪問 – user236501 2013-05-08 11:50:40

回答

0

請求頭應該是這樣的:

connection.setRequestProperty("Accept", "application/json"); 
connection.setRequestProperty("Content-Type", "application/json"); 
+0

嗨,即使我添加了2行,我仍然有這個錯誤{「錯誤」:{「product」:「不能爲空」 }} – user236501 2013-05-08 11:50:17

+0

您是否刪除了舊的「Content-Type」設置? – 2013-05-08 12:14:57

+0

已刪除,如果直接粘貼鏈接我可以訪問數據。 – user236501 2013-05-08 12:17:57