2016-09-12 8 views
0

誰でも正しい方向に向けることができますか、またはUnirestなしでAPIキーを使用してMashapeにリクエストを行う方法をデモンストレーションできますか?Mashape APIをVolleyまたはHttpURLConnectionで使用する

単にHttpURLConnectionクラスやOkHttpやVolleyのようなAndroid RESTライブラリを使用してJSONリクエストをMashape APIに作成したいと思っていますが、リクエストを構造化する方法や、Mashapeを使用せずにUnirestライブラリ。

HttpResponse<JsonNode> response = Unirest.get("https://wordsapiv1.p.mashape.com/words/incredible/definitions") 
    .header("X-Mashape-Key", "**********apikey************") 
    .header("Accept", "application/json") 
    .asJson(); 

イム痛みを設定するために、大きなCommonsWare自身が述べられているのでUnirestは、Androidのために避けるべきであるようにそれはそうなのでUnirestを避けるためにしよう:

は、これは、彼らがUnirest要求を作成することをお勧めする方法ですDoes anyone have an example of an android studio project that import Unirest via gradle?

私は実際には同じAPIを使用しようとすると、この問題の初心者と同じ状況にいます: Trying to fetch JSON with Android using Unirest

答えて

0

私の答えはあなたが探しているものだと私は信じています。

私はバレーのライブラリを使用し、あなたが依存関係を追加する必要があります。compile 'com.android.volley:volley:1.0.0'

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("MainActivity", "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); 
関連する問題