I am trying to send image to chatter post from my Android app and I am using below code to do this.
String url ="https://ap1.salesforce.com/services/data/v25.0/chatter/feeds/news/me/feed-items";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost;
MultipartEntity reqEntity;
httppost = new HttpPost(url);
reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File("/mnt/sdcard/save_icon.png");
NotepriseLogger.logMessage("File can read=="+file.canRead() + "--" + file.length());
/* File imageFile1 = new File(
"E:\\NotePrise\\Salesforce\\menu.jpg");*/
FileBody bin = new FileBody(file);
reqEntity.addPart("feedItemFileUpload", bin);
String fileName1 = "Chatter Image";
// file name can be text plain only, though using text/html doesn't breaks
reqEntity.addPart("fileName", new StringBody(fileName1, "text/plain",
Charset.defaultCharset()));
// Sending text/html doesn't helps as HTML will be printed, though using text/html doesn't breaks
reqEntity.addPart("text", new StringBody("Hello World", "text/plain",
Charset.defaultCharset()));
reqEntity.addPart("feedItemFileUpload", new FileBody(file,
fileName1, "application/octet-stream", Charset.defaultCharset()
.toString()));
httppost.setEntity(reqEntity);
httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
HttpResponse resp = httpclient.execute(httppost);
but I am getting following error
"09-10 13:58:18.118: V/Noteprise(13641): An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.
Thank you again for your patience and assistance. And thanks for using salesforce.com!
"
I ran the same code as a java standalone application and it is working fine so I assumed that httpclient used in android.jar file may be causing the issue and I am trying to build my own httpclient jar with different package name so I can use that in the code.
I don't know whether it is a correct approach or not.
Any input will be helpful for me.
How can I make a jar file of httpclient source code with different package . I did it but I am getting exception because of the logger classes in it.
I tried below method as well and getting this error "09-11 16:36:42.510: W/HttpMethodBase(4399): Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. 09-11 16:36:42.900: V/Noteprise(4399): [ { 09-11 16:36:42.900: V/Noteprise(4399): "message" : "Creating a text post requires text", 09-11 16:36:42.900: V/Noteprise(4399): "errorCode" : "MISSING_ARGUMENT" 09-11 16:36:42.900: V/Noteprise(4399): } ]returncode400 "
String url = "https://ap1.salesforce.com/services/data/v25.0/chatter/" +
"feeds/user-profile/me/feed-items";
String text = "I love posting files to Chatter!";
String desc = "This is a test file that I'm posting.";
Part[] parts = {
new StringPart("desc", desc),
new StringPart("fileName", fileName),
new StringPart("text", text),
new FilePart("feedItemFileUpload", imageFile),
};
final PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
postMethod.getParams()));
postMethod.setRequestHeader("Authorization", "Bearer " + salesforceRestClient.getAuthToken());
postMethod.setRequestHeader("Content-type", "x-www-form-urlencoded");
postMethod.addRequestHeader("X-PrettyPrint", "1");
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
httpClient.getParams().setSoTimeout(60000);
int returnCode = httpClient.executeMethod(postMethod);
String responseBody = postMethod.getResponseBodyAsString();
NotepriseLogger.logMessage(responseBody+"returncode"+returnCode);
} finally {
postMethod.releaseConnection();
}
I am trying below code to post image on user chatter wall
if (imageFile != null && fileName != null && imageTitle != null)
{
String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
Part[] parts = {
//new StringPart("desc", "Description"),
new StringPart("fileName", fileName),
//new StringPart("text", noteContent),
new StringPart("body", generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle)),
new FilePart("feedItemFileUpload", imageFile),
};
//String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
NotepriseLogger.logMessage(url);
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
//postMethod.setr(generateJSONBodyForChatterFeed(noteContent, null, null, null, null));
//postMethod.setRequestHeader("Content-type", "multipart/form-data");
//postMethod.addRequestHeader("X-PrettyPrint", "1");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.executeMethod(postMethod);
String responseBody = postMethod.getResponseBodyAsString();
NotepriseLogger.logMessage(responseBody);
generateJSONBodyForChatterFeed() method returning json String to me.
Here is the code of this method:
JSONArray msg = new JSONArray();
String bodyString = null;
try
{
if (mentionIds != null)
{
for (int i = 0; i < mentionIds.size(); i++)
{
JSONObject mention = new JSONObject();
mention.put("type", "mention");
mention.put("id", mentionIds.get(i));
msg.put(mention);
}
}
if (content != null)
{
JSONObject text = new JSONObject();
text.put("type", "text");
//content = URLEncoder.encode(content, "UTF-8");
text.put("text", " " + content);
msg.put(text);
}
JSONObject attachment = null;
if (fileName != null && imageTitle != null)
{
attachment = new JSONObject();
attachment.putOpt("desc", imageDescription);
attachment.putOpt("fileName", fileName);
attachment.putOpt("title", imageTitle);
}
JSONObject requestJSON = new JSONObject();
requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
if (attachment != null)
{
requestJSON.putOpt("attachment", attachment);
}
bodyString = requestJSON.toString();
NotepriseLogger.logMessage(bodyString);
generateJSONBodyForChatterFeed()
method?