Json “无效值:无法解析”谷歌预测api请求
我正在尝试使用谷歌预测API。我已经训练了我的模型,并通过网页测试了一个预测,效果非常好。然而,我现在正试图使用JavaAPI来预测一组记录,但我一直得到一个错误Json “无效值:无法解析”谷歌预测api请求,json,google-api,google-api-java-client,google-prediction,Json,Google Api,Google Api Java Client,Google Prediction,我正在尝试使用谷歌预测API。我已经训练了我的模型,并通过网页测试了一个预测,效果非常好。然而,我现在正试图使用JavaAPI来预测一组记录,但我一直得到一个错误 com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400, "errors" : [ { "domain" : "global", "message" : "Invalid
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'.",
"reason" : "invalid"
} ],
"message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'."
在我看来,json创建者似乎没有在特性周围加引号,但我尽可能地关注示例,它们不会更改或修改json工厂。这是凭证和预测构建代码
private static GoogleCredential authorize() throws Exception {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(PredictionScopes.PREDICTION))
.setServiceAccountPrivateKeyFromP12File(new File("p12filefromdevconsole.p12"))
.build();
return credential;
}
...
Prediction prediction = new Prediction.Builder(
httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
...
private static Output predict(Prediction prediction, String... features) throws IOException {
Input input = new Input();
InputInput inputInput = new InputInput();
inputInput.setCsvInstance(Collections.<Object>singletonList(features));
input.setInput(inputInput);
Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute();
return output;
}
有没有想过我做错了什么?经过多次挫折和反复尝试,我通过使用新的ArrayListAys.asListfeatures而不是Collections.singletonListfeatures解决了这个问题。这是修正的预测方法。请记住,我最初的实现直接来自谷歌网站上的示例:
private static Output predict(Prediction prediction, String... features) throws IOException {
Input input = new Input();
InputInput inputInput = new InputInput();
inputInput.setCsvInstance(new ArrayList(Arrays.asList(features)));
input.setInput(inputInput);
Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute();
return output;
}
在哪个版本的预测api中,是否存在InputInput类??我在任何版本中都找不到该类。请帮忙。。!!