Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java me:HttpConnection无法写入Instagram API请求的正文_Java_Rest_Java Me_Http Post_Instagram - Fatal编程技术网

java me:HttpConnection无法写入Instagram API请求的正文

java me:HttpConnection无法写入Instagram API请求的正文,java,rest,java-me,http-post,instagram,Java,Rest,Java Me,Http Post,Instagram,我有一个运行在J2ME上的应用程序需要访问Instagram API。具体来说,我需要发表评论。这是一种Post方法。因此,我尝试使用HttpConnection.setRequestProperty()将“text”参数添加到请求体中,但这似乎不起作用,因为Instagram无法识别该参数的存在。我认为这个方法无法将参数写入Http请求的主体。知道如何在j2me中实现这一点吗 这是我的密码: InputStream is = null; ByteArrayOutputStre

我有一个运行在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'"}}

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

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

请参阅此处的文档:。当我调用connection.getResponseCode()时,它会切换到已连接状态。在您提供的链接中,您应该阅读“使用带有HttpConnection的POST的示例”一段。我还是不明白你的输出流是从哪里来的。哦,我明白你现在说的了。非常感谢,这就是我需要的!