I'm trying to get the price of a Lyft for a certain path. Here is Lyft's authentication curl request, which is required to then request price:
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'
I believe the new android does not allow HTTP clients, so I'm super confused on how to make this request. My requests keep failing.
here is solution..
which works for me..Take a look may be it will be useful to you also..notify me if yes
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**