2013-05-08 147 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