代码之家  ›  专栏  ›  技术社区  ›  James Harpe

java-me:HttpConnection无法为Instagram API请求写入主体

  •  2
  • James Harpe  · 技术社区  · 12 年前

    我有一个在J2ME上运行的应用程序,需要访问Instagram API。具体来说,我需要发表评论。这是一种Post方法。因此,我尝试使用HttpConnection.setRequestProperty()将“text”参数添加到请求的主体中,但这似乎不起作用,因为Instagram无法识别该参数的存在。我认为这个方法无法将参数写入Http请求的主体。知道我如何在j2me中实现这一点吗?

    这是我的代码:

        InputStream is = null;
    
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
        byte[] response = null;
        HttpConnection connection = null;
    
        String url = "";
        try {
          url = "https://api.instagram.com/v1/media/" + mediaId + "/comments?access_token="+InstagramAPIUtil.accessTokenTest;// POST
          url = Util.urlEncodeSpaces(url);
    
    
            System.out.println(url);
            connection = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
    
            connection.setRequestMethod(HttpConnection.POST);
    
            connection.setRequestProperty("text", comment);
    
            if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                is = connection.openInputStream();
    
                if (is != null) {
                    int ch = -1;
    
                    while ((ch = is.read()) != -1) {
                        bos.write(ch);
                    }
    
                    response = bos.toByteArray();
                }
                System.out.println("response: "+new String(response));
                System.out.println("request: "+connection.getRequestProperty("text"));
                return true;
            }
    

    以下是我从Instagram上得到的信息:

    {"meta":{"error_type":"APIInvalidParametersError","code":400,"error_message":"Missing 'text'"}}
    
    1 回复  |  直到 12 年前
        1
  •  2
  •   mike    12 年前

    我在HTTP领域没有太多经验,但我认为您需要编写可以从中获得的输出流 connection 。我看不出您在代码中发送实际数据的位置。

      HttpURLConnection c = (HttpURLConnection) new URL("").openConnection();
      OutputStream out = c.getOutputStream();
      InputStream in = c.getInputStream();