public
Created

Sample code on how to use Unirest-java to call Mashape APIs in your Android projects

  • Download Gist
UnirestAndroid.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
public void sendMessage(View view) {
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
TextView txtView = (TextView) findViewById(R.id.textView1);
txtView.setText(message);
new CallMashapeAsync().execute(message);
}
private class CallMashapeAsync extends AsyncTask<String, Integer, HttpResponse<JsonNode>> {
protected HttpResponse<JsonNode> doInBackground(String... msg) {
HttpResponse<JsonNode> request = null;
try {
request = Unirest.get("https://webknox-question-answering.p.mashape.com/questions/answers?question=" + msg[0] + "&answerLookup=true&answerSearch=true")
.header("X-Mashape-Authorization", "<Insert your Mashape key here>")
.asJson();
} catch (UnirestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return request;
}
protected void onProgressUpdate(Integer...integers) {
}
protected void onPostExecute(HttpResponse<JsonNode> response) {
String answer = response.getBody().toString();
TextView txtView = (TextView) findViewById(R.id.textView1);
txtView.setText(answer);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.