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