2016-04-07 82 views
2

我被困了好幾天。 我想用informedica的API在我的Android應用程序。但我不知道如何與捲曲 其捲曲網址工作如何在android中使用curl url進行json api集成

捲曲-X GET --header「接受:應用/ JSON」 --header 「APP_ID:90f10c6a」 --header 「APP_KEY:9d23d9250d9f2ee8aa49efda732e4d3d」 「https://api.infermedica.com/v2/symptoms

請求URL是

https://api.infermedica.com/v2/symptoms

當我嘗試打開請求url鏈接我得到驗證參數丟失錯誤

請指導我!

在此先感謝!

+0

https://developer.infermedica.com/docs/authentication基本示例是否在粘貼憑據時起作用? –

+0

我無法理解這個例子。我不知道我會在哪裏添加我的憑據以進行回覆以及如何進行卷曲 –

+0

只需打開一個控制檯並在那裏運行它。我想你必須先學習控制檯和捲曲。 –

回答

1

使用Volley做cURL請求。

private void getResponse(){ 
    String URL = "https://api.infermedica.com/v2/symptoms"; 
    RequestQueue queue = Volley.newRequestQueue(this); 
    StringRequest request = new StringRequest(Request.Method.GET, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Log.e("Check Response",response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Check Error","Error"); 
       } 
      } 
    ){ 
     @Override 
     public byte[] getBody() throws AuthFailureError { 
      return new byte[]{}; 

     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String,String> map = new HashMap<>(); 
      map.put("Content-Type","application/json"); 
      map.put("Accept", "application/json"); 
      map.put("app_id","90f10c6a"); 
      map.put("app_key","9d23d9250d9f2ee8aa49efda732e4d3d"); 
      return map; 
     } 
    }; 
    request.setRetryPolicy(new DefaultRetryPolicy(15000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_MAX_RETRIES)); 
    queue.add(request); 
} 
+0

@Ahmad irfan,你這是對你的正確答案,然後接受答案。 –

0

這是我的函數來獲得JSON對象

public JSONObject getJSONFromUrl(String url) 
{ 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 


} 

如何添加你的代碼呢?