0

我正在使用 Apache abdera 将多部分请求发布到 IBM 连接 4.0 API。我从 Abdera API 得到空指针异常。请让我知道根本原因是什么。

private void createEntryWithAttachment(){
    try {
        String activityId = "urn:lsid:ibm.com:oa:662d0dc7-0308-48ee-8291-d730c733d2d1";         
        String activityIdLocal = activityId.substring(activityId.lastIndexOf(":")+1, activityId.length());
        String createEntryLocal = createEntry+activityIdLocal;      

        Abdera abdera = new Abdera();
        AbderaClient client = new AbderaClient(abdera);         
        AbderaClient.registerTrustManager();
        System.out.println("pd --->"+pd);
        client.addCookie("poktam2cl.iespc.ibm.com", "PD-S-SESSION-ID", pd, "/", null, true);

        RequestOptions requestOptions = client.getDefaultRequestOptions();
        requestOptions.setUseChunked(true);
        requestOptions.setHeader("Connection", "close");
        requestOptions.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");                        
        requestOptions.setContentType("multipart/related;type=\"application/atom+xml\"");
        requestOptions.setSlug("Sample.txt");


        Credentials credentials = new UsernamePasswordCredentials(username, password);
        client.addCredentials(createEntryLocal, AuthScope.ANY_REALM,AuthScope.ANY_SCHEME, credentials);

        Entry entry = abdera.getFactory().newEntry();
        entry.setTitle("create entry with attachment title ");
        entry.setContent("create entry with attachment content");

        javax.xml.namespace.QName field = new QName("http://www.ibm.com/xmlns/prod/sn", "field", "snx");
        org.apache.abdera.model.Element fieldElement = entry.addExtension(field);
        fieldElement.setAttributeValue("type", "file");
        fieldElement.setAttributeValue("name", "sampletextfile1");
        fieldElement.setAttributeValue("position", "3000");

        FileInputStream fis = new FileInputStream(filepath);
        requestOptions.setHeader("Content-Length", "35");

        entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type","entry", "Entry");

        ClientResponse response = client.post(createEntryLocal, entry, fis, "multipart/related;type=\"application/atom+xml\"", requestOptions );

        System.out.println("Entry Created with attachment's resp: " + response.getStatus());

        if(response.getStatus() == 201){
            System.out.println("Entry Created with attachment successfully .....");
            printIBMConnectionErrorMessage(response);
        }else{
            System.out.println("Entry with attachment creation failed");
            printIBMConnectionErrorMessage(response);
            //System.exit(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
} 

输出

java.lang.NullPointerException
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeRequest(MultipartRelatedRequestEntity.java:59)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at org.apache.abdera.protocol.client.AbderaClient.execute(AbderaClient.java:688)
at org.apache.abdera.protocol.client.AbderaClient.post(AbderaClient.java:306)
at JavaAgentEntryWithAttachment.createEntryWithAttachment(JavaAgentEntryWithAttachment.java:157)
at JavaAgentEntryWithAttachment.main(JavaAgentEntryWithAttachment.java:66)

此异常来自 abdera API,名为 MultipartRelatedRequestEntity.java 的类,第 74 行。我在下面放置了第 74 行源代码。所以很明显 contentSrc 是 null 并且 Abdera API 不允许我设置这个值。请让我知道我在这里缺少什么。

String contentId = entry.getContentSrc().toString();
4

2 回答 2

0

我分两步做:

  1. 发送文件
  2. 调用更新数据

每个都有很好的哑剧类型。您不能发送 XML mime 类型的文件。并放文件的长度。

于 2014-09-18T06:48:14.927 回答
0

可以避免空指针并在一个请求中完成。我遇到了同样的问题并创建了另一个问题并设法找到了解决方案。你可以在这里找到它。

它归结为以下代码示例,您可以在其中创建一个可以包含StringPartFilePart的HttpClient Part

  final Entry entry = // ... Create your Entry
  final RequestOptions options = this.client.getDefaultRequestOptions();
  options.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");

  StringPart entryPart = new StringPart("entry", entry.toString());
  entryPart.setContentType("application/atom+xml");

  FilePart filePart = new FilePart("file", new File(resource.getFile()));           

  RequestEntity request = new MultipartRequestEntity(new Part[] { entryPart, filePart}, this.client.getHttpClientParams());
  ClientResponse response = client.post(this.url + this.activityId, request, options);

希望这对将来使用 Abdera 的人们有所帮助。

于 2015-08-31T13:53:25.953 回答