I am trying to connect my android application to use the Tradeoff Analytics API. Here is part of the code I have used to do this:
private void showDilemma(final Dilemma dilemma) {
runOnUiThread(new Runnable() {
@Override public void run() {
tradeoffTextView.setText(dilemma.toString());
}
});
}
private class WatsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
showDilemma(tradeoffService.dilemmas(problem).execute());
return "processing tasks done";
}
@Override
protected void onPostExecute(String result) {
tradeoffTextView.setText("Tradeoff Analytics Status: " + result);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tradeoffButton = (Button) findViewById(R.id.tradeoffButton);
tradeoffTextView = (TextView) findViewById(R.id.tradeoffTextView);
tradeoffService = initTradeoffAnalytics();
problem = new Problem("phone");
String price = "price";
String ram = "ram";
String screen = "screen";
// Define the objectives
List<Column> columns = new ArrayList<Column>();
problem.setColumns(columns);
NumericColumn priceColumn = new NumericColumn();
**priceColumn.setKey(price);**
priceColumn.setGoal(Column.Goal.MIN);
priceColumn.setObjective(true);
priceColumn.range(0, 400);
priceColumn.setFullName("Price");
priceColumn.setFormat("number:2");
columns.add(priceColumn);
NumericColumn screenColumn = new NumericColumn();
**priceColumn.setKey(screen);**
priceColumn.setGoal(Column.Goal.MAX);
priceColumn.setObjective(true);
priceColumn.setFullName("Screen");
priceColumn.setFormat("number:2");
columns.add(screenColumn);
NumericColumn ramColumn = new NumericColumn();
**priceColumn.setKey(ram);**
priceColumn.setGoal(Column.Goal.MAX);
priceColumn.setFullName("RAM");
priceColumn.setFormat("number:2");
columns.add(ramColumn);
// Define the options to choose
List<Option> options = new ArrayList<Option>();
problem.setOptions(options);
HashMap<String, Object> galaxySpecs = new HashMap<String, Object>();
galaxySpecs.put(price, 50);
galaxySpecs.put(ram, 45);
galaxySpecs.put(screen, 5);
options.add(new Option("1", "Galaxy S4").values(galaxySpecs));
HashMap<String, Object> iphoneSpecs = new HashMap<String, Object>();
iphoneSpecs.put(price, 99);
iphoneSpecs.put(ram, 40);
iphoneSpecs.put(screen, 4);
options.add(new Option("2", "iPhone 5").values(iphoneSpecs));
HashMap<String, Object> optimusSpecs = new HashMap<String, Object>();
optimusSpecs.put(price, 10);
optimusSpecs.put(ram, 300);
optimusSpecs.put(screen, 5);
options.add(new Option("3", "LG Optimus G").values(optimusSpecs));
tradeoffButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WatsonTask task = new WatsonTask();
task.execute(new String[]{});
}
});
}
When I try to execute this code, the application is crashing and I am getting the following error message: E/WatsonService: POST https://gateway.watsonplatform.net/tradeoff-analytics/api/v1/dilemmas, status: 400, error: Data supplied is missing critical information. all objectives must have keys
I am not sure why I am getting this error message seeing as I have checked all the objectives ( price, ram and screen) and they each have a set key. The code is also very similar to that available in the online documentation so I am not sure where I have gone wrong. Any help would be much appreciated, thanks in advance :)
Answer by DavidAmid (644) | Dec 09, 2016 at 02:06 AM
You are setting the priceColumn each time (same goes for RAM) its a copy paste error
THIS IS SCREEN COLUMN NumericColumn screenColumn = new NumericColumn();
THIS IS NOT SCREEN COLUMN **priceColumn.setKey(screen);**
THIS IS NOT SCREEN COLUMN priceColumn.setGoal(Column.Goal.MAX);
THIS IS NOT SCREEN COLUMN priceColumn.setObjective(true);
THIS IS NOT SCREEN COLUMN priceColumn.setFullName("Screen");
THIS IS NOT SCREEN COLUMN priceColumn.setFormat("number:2");
THIS IS SCREEN COLUMN columns.add(screenColumn);
ANDROID APPLICATION TO CHAT WITH IBM BLUEMIX DEVICE 1 Answer
Can Tradeoff Analytics be used to evaluate the best possible grouping of independent problems? 5 Answers
Can't access Watson API locally due to CORS 2 Answers
Use Concept Insights to send text from an image and get a response related to the text in IOS swift 1 Answer