Ich habe eine Frage Funktionalität in meiner Anwendung, in der der Benutzer eine Frage in eine Editext-Zeile eingibt und die API die Antwort abruft. Wenn der Benutzer submit schlägt, möchte ich einen Alarmdialog mit der Antwort anzeigen. Ab sofort werden sie auf einen anderen Bildschirm mit nur dem angezeigten Antworttext geleitet. Ich möchte, dass die Antwort stattdessen in einer Textansicht in einem Warndialog angezeigt wird. Kann jemand bitte helfen. Das Folgende ist mein Code für die Frageklasse.TextView in Alert-Dialog
Question.java
public class Question extends ActionBarActivity {
String api = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/quickAnswer?";
private String question;
private String answer;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String unFilteredQuestion = extras.getString("question");
question = unFilteredQuestion.replaceAll("\\s+", "%20");
System.out.println(question);
new query().execute();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_question, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class query extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Question.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
JSONObject jsonStr = null;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(api+"q=" + question);
httppost.setHeader("X-Mashape-Authorization", "5VSMYMsFj4msh0QQAjh7CCxfTaQqp1WVtbmjsnGgPs5B2mmY5k");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
String responseBody = httpclient.execute(httppost, responseHandler);
jsonStr = new JSONObject(responseBody);
}catch (Exception e){e.printStackTrace();}
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
answer = jsonStr.getString("answer");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
TextView text = (TextView)findViewById(R.id.answer);
text.setText(answer);
}
}
}