Skip to content

Commit 0b5240d

Browse files
author
spoeck
committed
fix: solved #13: return json object instead of string
1 parent 8f0f061 commit 0b5240d

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

android/src/main/java/de/innfactory/apiai/RNApiAiModule.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.HashMap;
1515
import java.util.Map;
1616
import java.util.List;
17-
import org.json.JSONObject;
1817

1918
import ai.api.AIListener;
2019
import ai.api.AIServiceException;
@@ -123,7 +122,7 @@ public void setEntitiesAsJson(String userEntitiesAsJson) throws AIServiceExcepti
123122

124123

125124
@ReactMethod
126-
public void startListening(Callback onResult, Callback onError) {//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
125+
public void startListeningNative(Callback onResult, Callback onError) {//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
127126

128127
onResultCallback = onResult;
129128
onErrorCallback = onError;
@@ -187,7 +186,7 @@ public void onResult(AIResponse response) {
187186
if (onResultCallback != null) {
188187
Gson gson = new Gson();
189188
try {
190-
onResultCallback.invoke(new JSONObject(gson.toJson(response)));
189+
onResultCallback.invoke(gson.toJson(response));
191190
} catch (Exception e) {
192191
Log.e(TAG, e.getMessage(), e);
193192
}
@@ -202,7 +201,7 @@ public void onError(final AIError error) {
202201
Gson gson = new Gson();
203202

204203
try {
205-
onErrorCallback.invoke(new JSONObject(gson.toJson(error)));
204+
onErrorCallback.invoke(gson.toJson(error));
206205
} catch (Exception e) {
207206
Log.e(TAG, e.getMessage(), e);
208207
}
@@ -277,7 +276,7 @@ public void onAudioLevel(float level) {
277276

278277

279278
@ReactMethod
280-
public void requestQuery(String query, Callback onResult, Callback onError) {
279+
public void requestQueryNative(String query, Callback onResult, Callback onError) {
281280

282281
onResultCallback = onResult;
283282
onErrorCallback = onError;
@@ -314,7 +313,7 @@ protected AIResponse doInBackground(AIRequest... requests) {
314313
} catch (AIServiceException e) {
315314
Gson gson = new Gson();
316315
try {
317-
onErrorCallback.invoke(new JSONObject(gson.toJson(e)));
316+
onErrorCallback.invoke(gson.toJson(e));
318317
} catch (Exception e1) {
319318
Log.e(TAG, e.getMessage(), e);
320319
}

example/index.android.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ export default class App extends Component {
8888

8989
ApiAi.startListening(result => {
9090
console.log(result);
91-
this.setState({result: result});
91+
this.setState({result: JSON.stringify(result)});
9292
}, error => {
93-
this.setState({result: error});
93+
this.setState({result: JSON.stringify(error)});
9494
});
9595

9696
}}/>

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"react": "16.0.0-alpha.12",
1111
"react-native": "0.47.1",
12-
"react-native-api-ai": "^1.3.0"
12+
"react-native-api-ai": "../"
1313
},
1414
"devDependencies": {
1515
"babel-jest": "20.0.3",

index.android.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import EventRequest from './js/ResetContextsRequest';
55

66
let ApiAi = NativeModules.ApiAi;
77

8+
ApiAi.startListening = (onResult: ()=>{}, onError: ()=>{}) => {
9+
ApiAi.startListeningNative((r)=>onResult(JSON.parse(r)), (e)=>onError(JSON.parse(e)))
10+
};
11+
12+
ApiAi.requestQuery = (query: String, onResult: ()=>{}, onError: ()=>{}) => {
13+
ApiAi.requestQueryNative(query, (r)=>onResult(JSON.parse(r)), (e)=>onError(JSON.parse(e)))
14+
};
15+
816
ApiAi.setContexts = (contexts) => {
917
ApiAi.setContextsAsJson(JSON.stringify(contexts))
1018
};

0 commit comments

Comments
 (0)