我正在使用dialogflow
制作一个Android聊天机器人。我已经开始使用webhook功能了。问题是应用程序不断崩溃
我收到此错误:
3-23 20:40:13.121 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:状态类型:partial_content 03-23 20:40:13.121 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:已解决的查询:hi 03-23 20:40:13.121 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:动作:smalltalk.greetings.hello 03-23 20:40:13.122 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:演讲:嘿........... 03-23 20:40:13.122 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:Intent id:null 03-23 20:40:13.122 25844-25844 / com.khatkhatay.aqid.chatbot I / com.khatkhatay.aqid.chatbot.MainActivity:意图名称:null 03-23 20:40:13.122 25844-25844 / com.khatkhatay.aqid.chatbot D / a:just recahde 03-23 20:40:13.122 25844-25844 / com.khatkhatay.aqid.chatbot D / AndroidRuntime:关闭VM 03-23 20:40:13.124 25844-25844 / com.khatkhatay.aqid.chatbot E / AndroidRuntime:FATAL EXCEPTION:main 过程:com.khatkhatay.aqid.chatbot,PID:25844 java.lang.NullPointerException:尝试调用虚方法' int java.lang.String.hashCode()'在null对象引用上 在com.khatkhatay.aqid.chatbot.MainActivity $ 3.run(MainActivity.java:176) 在android.app.Activity.runOnUiThread(Activity.java:6175) 在com.khatkhatay.aqid.chatbot.MainActivity.onResult(MainActivity.java:138) 在com.khatkhatay.aqid.chatbot.MainActivity.access $ 200(MainActivity.java:39) 在com.khatkhatay.aqid.chatbot.MainActivity $ 2.onPostExecute(MainActivity.java:127) 在com.khatkhatay.aqid.chatbot.MainActivity $ 2.onPostExecute(MainActivity.java:104) 在android.os.AsyncTask.finish(AsyncTask.java:695) 在android.os.AsyncTask.-wrap1(未知来源:0) 在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:712) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6809) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 03-23 20:40:13.124 25844-25844 / com.khatkhatay.aqid.chatbot D / AppTracker:应用事件:崩溃
我的代码:
public static final String TAG = MainActivity.class.getName();
ListView conversationList;
ImageButton sendButton;
EditText userInput;
AIConfiguration aiConfiguration;
AIDataService aiDataService;
public static ArrayList<ChatData> chatlist;
public static ChatAdapter chatAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AIConfiguration.SupportedLanguages lang =
AIConfiguration.SupportedLanguages.fromLanguageTag("en");
aiConfiguration = new AIConfiguration("2f41a8be61ab4b70acf9b26e78ab8577",lang);
aiDataService = new AIDataService(aiConfiguration);
sendButton = (ImageButton)findViewById(R.id.imageButton);
userInput = (EditText)findViewById(R.id.inputMessage);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = userInput.getText().toString();
if(!text.trim().equals("")) {
addMessage(text,true);
userInput.setText("");
sendRequest(text);
}
}
});
conversationList = (ListView) findViewById(R.id.ConversationList);
conversationList.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
conversationList.setStackFromBottom(true);
chatlist = new ArrayList<ChatData>();
chatAdapter = new ChatAdapter(this, chatlist);
conversationList.setAdapter(chatAdapter);
}
private void addMessage(String message, boolean isMine) {
final ChatData chatMessage = new ChatData("", "","", isMine);
chatMessage.body = message;
chatAdapter.add(chatMessage);
chatAdapter.notifyDataSetChanged();
}
private void sendRequest(String userText) {
final String contextString = String.valueOf(userText);
final AsyncTask<String, Void, AIResponse> task = new AsyncTask<String, Void, AIResponse>() {
private AIError aiError;
@Override
protected AIResponse doInBackground(final String... params) {
final AIRequest request = new AIRequest();
String query = params[0];
if (!TextUtils.isEmpty(query))
request.setQuery(query);
try {
return aiDataService.request(request);
} catch (final AIServiceException e) {
aiError = new AIError(e);
return null;
}
}
@Override
protected void onPostExecute(final AIResponse response) {
if (response != null) {
onResult(response);
} else {
onError(aiError);
}
}
};
task.execute(contextString);
}
private void onResult(final AIResponse response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "onResult");
Log.i(TAG, "Received success response");
// this is example how to get different parts of result object
final Status status = response.getStatus();
Log.i(TAG, "Status code: " + status.getCode());
Log.i(TAG, "Status type: " + status.getErrorType());
final Result result = response.getResult();
Log.i(TAG, "Resolved query: " + result.getResolvedQuery());
Log.i(TAG, "Action: " + result.getAction());
final String speech = result.getFulfillment().getSpeech();
Log.i(TAG, "Speech: " + speech);
if (!TextUtils.isEmpty(speech))
addMessage(speech,false);
final Metadata metadata = result.getMetadata();
if (metadata != null) {
Log.i(TAG, "Intent id: " + metadata.getIntentId());
Log.i(TAG, "Intent name: " + metadata.getIntentName());
}
final HashMap<String, JsonElement> params = result.getParameters();
if (params != null && !params.isEmpty()) {
Log.i(TAG, "Parameters: ");
for (final Map.Entry<String, JsonElement> entry : params.entrySet()) {
Log.i(TAG, String.format("%s: %s", entry.getKey(), entry.getValue().toString()));
}
}
switch (metadata.getIntentName()){
case "Weather":
getWeatherData(params);
break;
}
}
});
}
private void parseData(JSONObject json){
try {
if(json.getJSONObject("query").getInt("count") > 0){
String details = "";
JSONObject channel = json.getJSONObject("query").getJSONObject("results").getJSONObject("channel");
details = "Weather looks " + channel.getJSONObject("item").getJSONObject("condition").getString("text") +
" in " + channel.getString("title").replace("Yahoo! Weather - ","");
details = details + "\nTemprature is " + channel.getJSONObject("item").getJSONObject("condition").getString("temp")
+ (char) 0x00B0 + "F.";
addMessage(details,false);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getWeatherData(HashMap<String, JsonElement> params) {
String geoCity = "";
if (params != null && !params.isEmpty()) {
Log.i(TAG, "Parameters: ");
for (final Map.Entry<String, JsonElement> entry : params.entrySet()) {
Log.i(TAG, String.format("%s: %s", entry.getKey(), entry.getValue().toString()));
if(entry.getKey().equals("geo-city"))
geoCity = entry.getValue().getAsString();
}
}
final AsyncTask<String, Void, JSONObject> task = new AsyncTask<String, Void, JSONObject>() {
private AIError aiError;
JSONObject weatherData = null;
@Override
protected JSONObject doInBackground(final String... params) {
final AIRequest request = new AIRequest();
String query = params[0];
if (!TextUtils.isEmpty(query)) {
try {
URL url = new URL("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + query + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"); //Enter URL here
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
InputStream in = new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
String sb = "";
while ((line = reader.readLine()) != null) {
sb = sb + line;
}
JSONObject myJsonArray = new JSONObject(sb);
//JSONObject jsonObj = new JSONObject(sb);
weatherData = myJsonArray;
} catch (final Exception e) {
return null;
}
}
return weatherData;
}
@Override
protected void onPostExecute(final JSONObject response) {
if (response != null) {
parseData(response);
} else {
}
}
};
task.execute(geoCity);
}
private void onError(final AIError error) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addMessage(error.toString(),false);
}
});
}
}