2016-03-05 56 views
0

試圖在機器人應用程序,它似乎只接受使用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(); 

麻煩正在執行與AsyncTaskdoInBackground的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結構的要求。 可以做到這一點嗎?

+1

返回doInBackground()方法和onPostExecute中的響應對象,您可以將結果鏈接到UI線程中的任何視圖。 – Inducesmile

+0

@NollyJ我明白,但不完全確定如何去做:S –

回答

2

這段代碼沒有經過測試,但給你一個關於如何解決這個問題的想法。

private class TastingUniRestAsync extends AsyncTask<String, Void, String> { 

    protected String doInBackground(String... urls) { 
     String pathToFile = urls[0]; 
     String responseResult = ""; 
     HttpResponse<JsonNode> response = Unirest.get(pathToFile) 
       .header("X-Mashape-Key", "**********apikey************") 
       .header("Accept", "application/json") 
       .asJson(); 

     if(null != response){ 
      //convert your response to the data type you want. Here I am using string 
      responseResult = //assign the manipulated Json string; 
     } 
     return responseResult 
    } 
    protected void onPostExecute(String responseResult){ 
     // You can assign it to TextView widget for example 
     mTextView.setText(responseResult); 
    } 
} 
1

我認爲更好的解決方案是使用Volley庫。看看我的解決方案,不要忘記添加依賴項:compile 'com.android.volley:volley:1.0.0'。如果您需要more information

RequestQueue requestQueue = Volley.newRequestQueue(this); 
String uri = Uri.parse("https://wordsapiv1.p.mashape.com/words/incredible/definitions") 
    .buildUpon() 
    .build().toString(); 

StringRequest stringRequest = new StringRequest(
    Request.Method.GET, uri, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.d("VolleyResponse", "response: " + response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("VolleyError", error.toString()); 
     } 
    }) { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> params = new HashMap<>(); 
      params.put("X-Mashape-Key", "<API_KEY>"); 
      params.put("Accept", "text/plain"); 
      return params; 
     } 
    }; 
    requestQueue.add(stringRequest);