【问题标题】:IBM Worklight- HTTP adapterIBM Worklight - HTTP 适配器
【发布时间】:2016-01-03 08:26:32
【问题描述】:

如何创建 HTTP 适配器以在 Web 主机上检索文件。 我很困惑,因为 HTTP 适配器用于检索用于 RSS 提要的 json 输出。我如何定位文件(例如 .jpg)。

谢谢。

【问题讨论】:

  • jpg 路径应该在 json 输出中返回的响应中,然后您可以获取文件。
  • @Hussam Eddin 那么如何抓取文件呢?
  • Idan 的回答准确地解释了如何获取它。我正在按照相同的步骤将其添加到我的项目中。
  • 如何在worklight中使用apache cordova?
  • 查看我的回答,请注意我们不需要教你如何编码。您需要尝试一下,我们可以为您解决问题并为您提供帮助。

标签: javascript ibm-mobilefirst worklight-adapters


【解决方案1】:

扩展伊丹的答案:

将远程图像 URL 作为参数提供给适配器:

适配器 JS:

function getImage() {
WL.Logger.info("###################  getImage  ######################");

var val = com.company.ProjectName.ImageEncoder.getImage("http://Some-Domain/../.../id.gif");

WL.Logger.info("###################  IMAGE IS  ######################");
WL.Logger.info(val);
WL.Logger.info("#####################################################");
var imageData = {"data":val};
WL.Logger.info(JSON.stringify(imageData));
return imageData;
}

适配器 XML:

将过程添加到适配器:

<procedure name="getImage"/>

将自定义 Java 代码添加到您的服务器:

Java 代码:

package com.company.ProjectName;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.MalformedInputException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class ImageEncoder {

    public static String getImage(String imageUrl)
            throws MalformedURLException, IOException {

        String imageDataString = "";
        URL url = null;
        int i;
        try {
            url = new URL(imageUrl);
            System.out.println(imageUrl);



            HttpURLConnection connection = null; 
            String protocol = url.getProtocol(); 
            System.out.println(protocol);
          // this is to trust any certificates from the target server 
          if("https".equalsIgnoreCase(protocol)){ 
                      // Create a trust manager that does not validate certificate chains 
              System.out.println("inside If");
                      TrustManager[] trustAllCerts = new TrustManager[]{ 
                          new X509TrustManager() { 
                              public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
                                  return null; 
                              } 
                              public void checkClientTrusted( 
                                  java.security.cert.X509Certificate[] certs, String authType) { 
                              } 
                              public void checkServerTrusted( 
                                  java.security.cert.X509Certificate[] certs, String authType) { 
                              } 
                          } 
                      }; 

                      // Install the all-trusting trust manager 
                SSLContext sc = SSLContext.getInstance("SSL"); 
                sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

              connection = (HttpsURLConnection)url.openConnection(); 
              System.out.println("connection"+connection.getContentLength()); 
                }else{ 
                  connection=(HttpURLConnection) url.openConnection(); 

          } 


            InputStream input = connection.getInputStream(); 


            byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(input);

            input.close();
            imageDataString = encodeImage(bytes);

            return imageDataString;


        } catch (MalformedInputException malformedInputException) {
            malformedInputException.printStackTrace();
            imageDataString = malformedInputException.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
        } catch (IOException ioException) {
            ioException.printStackTrace();
            imageDataString = ioException.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
        }  catch (NoSuchAlgorithmException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
            imageDataString = e.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
    } catch (KeyManagementException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    imageDataString = e.toString();
    return ("exception while reading the imag <" + imageDataString + ">");
} 



    public static String encodeImage(byte[] imageData) {
        // TODO Auto-generated method stub
        org.apache.commons.codec.binary.Base64 base = new org.apache.commons.codec.binary.Base64(
                false);
        return base.encodeToString(imageData);

    }
}

Java 代码目录:

注意:不要信任所有证书。您需要添加自己的信任经理。这仅用于测试

【讨论】:

    【解决方案2】:

    您可以按照in this blog post提供的说明进行操作。

    步骤如下:

    1. 将远程图像 URL 作为参数提供给适配器
    2. 使用 Java 实用程序在服务器上对返回的图像进行 Base64 编码
    3. 将编码后的 base64 字符串返回给应用程序
    4. Base64 解码字符串并显示图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      相关资源
      最近更新 更多