问题描述
我需要为worklight创建一个HTTP适配器,但必须通过参数以编程方式提供url。
I need to create an HTTP adapter for worklight but the url must be programmatically provided via a parameter.
1)我能够传递用户/密码但是不是网址。有没有办法做到这一点?
1) I was able to pass the user/password but not the url. Is there a way to do that?
我也尝试创建自己的java适配器来调用REST API,它在我测试适配器时有效,但似乎是我的响应不是工作灯的预期格式。我收到此错误:
I also try to create my own java adapter to call the REST API, It works when I test the adapter but it seems my response is not in the expected format for worklight. I got this error:
2)BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENT。
2) BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENT.
我的Java适配器返回JSONArtifact(JSONObject)但它似乎worklight希望将它嵌入到另一个JSONObject中,例如{array:{...}}。
有没有办法将JSONObject转换为worklight所期望的格式。
my Java adapter returns a JSONArtifact (JSONObject) but it seems that worklight want this to be embedded in another JSONObject such as { "array":{...}}.Is there a way to convert a JSONObject to the format expected by worklight.
import org.apache.wink.json4j.JSON;
import org.apache.wink.json4j.JSONArtifact;
import org.apache.wink.json4j.JSONException;
private Header headerUserAgent = new Header("User-Agent", "Mozilla");
private Header headerAccept = new Header("Accept", "application/json");
private String hostName;
private String baseURL;
protected MyHttpClient(String userName, String userPassword, String hostName, String baseURL ) {
super();
Credentials defaultcreds = new UsernamePasswordCredentials(userName,
userPassword);
this.getState().setCredentials(AuthScope.ANY, defaultcreds);
this.hostName = hostName;
this.baseURL = baseURL;
}
private GetMethod getGetMethod(String url) throws URIException {
GetMethod httpMethod = new GetMethod(new HttpsURL("https://"+hostName+baseURL+url).getEscapedURI());
addCommonHeaders(httpMethod);
return httpMethod;
}
private JSONArtifact getResponseAsJSONObject(InputStream inputStream) throws IOException {
InputStreamReader reader = new InputStreamReader(inputStream);
try {
JSONArtifact json = JSON.parse(reader);
return json;
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
适配器:
function getResponse(user,password) {
var client = new com.itdove.mypackage.MyHttpClient(user,password,"myurl","mybaseurl");
return {
array : client.executeGet("mypath")
};
}
它适用于此但此解决方案不提供服务URL作为参数:
it works with this but this solution doesn't provide the service url as parameter:
function getResponseAdapters(path, username, password) {
var input = {
method : 'get',
returnedContentType : 'json',
headers: {
'User-Agent':'Mozilla',
'Authorization': 'Basic '+Base64.encode(username+':'+password),
} ,
path : '/resources/' + path
};
return WL.Server.invokeHttp(input);
}
function getResponse(username, password) {
return getMySCAWSAdapters(path, username, password);
}
收藏
vAPPArrayAdapterOptions = {
name: 'myResponseAdapter',
replace: '',
remove: '',
add: '',
load: {
procedure: 'getResponse',
params: ["user","password"],
key: 'array'
},
accept: function (data) {
return (data.status === 200);
}
},
...
vAPPArray = wlJsonStore.initCollection(
"vAPPArray",
vAPPArraySearchFields,
{adapter: vAPPArrayAdapterOptions,
onSuccess: initCollectionSuccessCallback,
onFailure: initCollectionFailureCallback,
load:true});
非常感谢
Dominique
Many ThanksDominique
推荐答案
找到解决方案:
首先,我使用的是apache wink JSONArtifact而不是com.ibm.json.java.JSONArtifact!
First, I was using apache wink JSONArtifact instead of the com.ibm.json.java.JSONArtifact!
其次我修改了我的收集器实现方法,如下所示添加状态(不确定是否需要)
Secondly I modified my collector implement method as follow to add the status (not sure if it is needed or not)
function getResponse(user,password,hostname) {
var client = new com.itdove.mypackage.IWDHttpClient(user,password,hostname,"mypath");
return {
array :client.executeGet("mymethod"),
statusCode: client.getStatusCode(),
statusReason: client.getStatusReason()
};
}
myCollector.js中的
我在调用initCollection之前设置了用户,密码和主机名。
in myCollector.js I set the user, password, hostname as follow before calling my initCollection.
params = [ settings.json.user, settings.json.password, settings.json.hostname ];
myAdapterOptions.load["params"] = params;
这篇关于在Worklight适配器中更改HTTP URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!