试图在机器人应用程序,它似乎只接受使用Unirest请求使用这种“词API”与Android获取JSON。为 “不可思议”(由API指定)的定义尝试使用Unirest
请求例如:
HttpResponse<JsonNode> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/incredible/definitions")
.header("X-Mashape-Key", "**********apikey************")
.header("Accept", "application/json")
.asJson();
麻烦正在执行与AsyncTask
doInBackground
的unirest请求。
protected void OnPreExecute(){
json_url = "https://wordsapiv1.p.mashape.com/words/incredible/definitions";
//where does api key go?
}
protected String doInBackground(Void... params) {
try {
// unirest goes here but how?
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_STRING = bufferedReader.readLine()) !=null)
stringBuilder.append(JSON_STRING+"\n");
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
}catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
不知道到底如何内doInBackground
结构的要求。 可以做到这一点吗?
返回doInBackground()方法和onPostExecute中的响应对象,您可以将结果链接到UI线程中的任何视图。 – Inducesmile
@NollyJ我明白,但不完全确定如何去做:S –