Lyft API Android 集成?

Posted

技术标签:

【中文标题】Lyft API Android 集成?【英文标题】:Lyft API Android integration? 【发布时间】:2016-02-26 02:02:21 【问题描述】:

我正在尝试获取特定路径的 Lyft 价格。这是 Lyft 的身份验证 curl 请求,然后请求价格:

curl -X POST -H "Content-Type: application/json" \
     --user "<client_id>:<client_secret>" \
     -d '"grant_type": "client_credentials", "scope": "public"' \
     'https://api.lyft.com/oauth/token'

我相信新的 android 不允许 HTTP 客户端,所以我对如何发出这个请求感到非常困惑。我的请求不断失败。

【问题讨论】:

【参考方案1】:

这里有解决方案..

这对我有用..看看可能对你也有用..如果是请通知我

private void LyftCabAPI()//to get uber cab info in given source and destination
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> 
        @Override
        protected void onPreExecute() 
            super.onPreExecute();
            pDialog2 = new ProgressDialog(MainActivity.this);//
            pDialog2.setMessage("Searching Cabs.....");
            pDialog2.setIndeterminate(false);
            pDialog2.setCancelable(true);
            pDialog2.show();
        
        @Override
        protected  String doInBackground(String...params) 
            try 
                /************** For getting response from HTTP URL start ***************/
                String url="https://api.lyft.com/oauth/token";
                URL object = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) object.openConnection();
                connection.setReadTimeout(60 * 1000);
                connection.setConnectTimeout(60 * 1000);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setDoOutput(true);
                String authorization="client id"+":"+"client secret";
                byte[] __data = authorization.getBytes("UTF-8");
                String base64 = Base64.encodeToString(__data, Base64.DEFAULT);
                String encodedAuth="Basic "+base64;
                connection.setRequestProperty("Authorization", encodedAuth);

                String data =  "\"grant_type\": \"client_credentials\", \"scope\": \"public\"";
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                out.write(data);
                out.close();

                connection.setRequestMethod("POST");
                int responseCode = connection.getResponseCode();
                //String responseMsg = connection.getResponseMessage();
                //__LfytAccessResponse=responseMsg;
                Log.v("LyftAccess", "httpStatus :" + responseCode);
                if (responseCode == 200) 
                    InputStream inputStr = connection.getInputStream();
                    String encoding = connection.getContentEncoding() == null ? "UTF-8"
                            : connection.getContentEncoding();
                    __LfytAccessResponse = IOUtils.toString(inputStr, encoding);
                
             catch (Exception e) 
                e.printStackTrace();
            
            return "success";
        
        @Override
        protected void onPostExecute(String result) 
            super.onPostExecute(result);

            Log.i(" __LfytAccessResponse", __LfytAccessResponse);
            try 
                JSONObject jsonObject = new JSONObject(__LfytAccessResponse);
                accessToken = jsonObject.getString("access_token");
              Toast.makeText(getApplicaationContext(),"Access Token:"+                              accessToken,Toast.LENGTH_SHORT).show();
             catch (JSONException e)
                e.printStackTrace();
            
        
    
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute();


 **Happy Coding**

【讨论】:

以上是关于Lyft API Android 集成?的主要内容,如果未能解决你的问题,请参考以下文章

Lyft Android SDK 集成问题

如何使用 Mystro 安卓应用等 Lyft 驱动程序 API?

Lyft 是不是会限制对整个应用程序 API 的请求?

在 Lyft API 中付款

汇总集成环信遇到的问题

Lyft API 取消行程不起作用