无法将图像或附件上载到Salesforce chatter Rest API

时间:2016-07-25 13:14:21

标签: android rest salesforce-chatter

我正在我的应用程序中集成Salesforce。我需要在我的应用程序中显示 Chatter提要,在此过程中,我已经完成了Chatter Rest Api的所有功能(发布,获取数据,评论,喜欢等),但我无法上传任何类型的附件到聊天API

我咨询过以下链接:

https://github.com/gauravkheterpal/boxForce/blob/master/src/com/metacube/boxforce/SalesForceObjectChooser.java

https://github.com/noeticpenguin/CasePhotographer/blob/master/src/com/brightleafsoftware/casephotographer/CaseDetailFragment.java

How do I upload file using Salesforce Mobile SDK for android?

我的代码:

 private void postImageAsAttachment(String parentId, String title,String filepath) {
        Map<String, Object> fields = new HashMap<String, Object>();
        fields.put("Name", title);
        fields.put("ParentID", "The user id");
        File file=new File(filepath);
        fields.put("Body", encodeFileInBase64(file));
        fields.put("ContentType", "image/jpeg");

        RestRequest request = null;
        try {
            request = RestRequest.getRequestForCreate(getString(R.string.api_version), "Attachment", fields);
            System.out.println("Request File:"+request);
        } catch (Exception ex) {
            Log.d(TAG, "sendRequest: ", ex);
            Toast.makeText(MyNewsFeed.this, "The file upload failed: " + ex.toString(), Toast.LENGTH_LONG).show();
        }
        client.sendAsync(request, new RestClient.AsyncRequestCallback() {

            @Override
            public void onSuccess(RestRequest request, RestResponse result) {

                try {
                    //Do something with JSON result.
                    // System.out.println("Restresponse--->"+result);
                    println(result);  //Use our helper function, to print our JSON response.
                } catch (Exception e) {
                    e.printStackTrace();
                }
                EventsObservable.get().notifyEvent(EventsObservable.EventType.RenditionComplete);
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
                EventsObservable.get().notifyEvent(EventsObservable.EventType.RenditionComplete);
            }
        });
    } 

我在获取文件路径之后从onActivityResult 调用此方法,我想在Chatter中上传。

以下方法将图像编码为Base64

 private String encodeFileInBase64(File file) {
        String fileTxt="";
        StringBuilder sb = new StringBuilder();
        // StringBuilder sbStr = new StringBuilder((int) (file.length() / 3 *
        // 8));

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);

            int bSize = 3000; // 3 * 512;
            Log.v("fileLength", String.valueOf(file.length()));

            byte[] buf = new byte[bSize];

            int len = 0;

            while ((len = fin.read(buf)) != -1) {
                Log.v("len", String.valueOf(len));// sbStr.append(new
                // String(buf, 0, len));

                byte[] encoded = Base64.encode(buf, Base64.DEFAULT);

                sb.append(new String(encoded, 0, len));
            }

            Log.v("txtFileWithBuffer", sb.toString());

            fin.close();
        } catch (IOException e) {
            if (null != fin) {
                try {
                    fin.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }


            String base64EncodedFile = sb.toString();


            FileInputStream fileInputStream = null;

            byte[] bFile = new byte[(int) base64EncodedFile.length()];

            try {
                fileInputStream = new FileInputStream(base64EncodedFile);
                fileInputStream.read(bFile);
                fileInputStream.close();
                fileTxt = Base64.encodeToString(bFile, Base64.DEFAULT);
                Log.v("txtFileWithoutBuffer", fileTxt);

                return fileTxt;

                // encodeImgList.add(encodedImage);

            } catch (Exception ex) {

                ex.printStackTrace();

                return "";
            }

            // return base64EncodedFile;

        }
        return fileTxt;
    }

每当我执行该方法时,它都会给我错误:

BasicNetwork.performRequest: Unexpected response code 400 for https://ap2.salesforce.com/services/data/v36.0/sobjects/Attachment
07-25 18:41:39.176 26386-26386/com.salesforce.th W/System.err: com.android.volley.ServerError
07-25 18:41:39.176 26386-26386/com.salesforce.th W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:179)
07-25 18:41:39.177 26386-26386/com.salesforce.th W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

我知道只有在格式错误的XML或JSON正文时才会出现此错误。但我没有发现我在这里做错了什么。

我无法理解我在这里缺少的东西。请帮助。

1 个答案:

答案 0 :(得分:0)

我注意到的一件事是不正确的

  

fields.put(&#34; ParentID&#34;,&#34;用户ID&#34;);

应该是变量parentId而不是