【发布时间】:2017-04-18 03:33:32
【问题描述】:
我集成了 uber 乘车请求 api。我成功验证了 uber 帐户。我可以从 uber api 获取用户历史记录和用户个人资料,但我没有得到 v1.2/requests/estimate。但是当我请求搭车时。使用下面的代码...
我得到了回应。
{"message":"提供的 OAuth 2.0 凭据无效。","code":"unauthorized"}
public JSONObject getFareid(String address,String product_id,floatstart_latitude,float start_longitude,float end_latitude,floatend_longitude,String token,String scope) {
try {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(address);
params.add(new BasicNameValuePair("product_id", product_id));
params.add(new BasicNameValuePair("start_latitude", "17.456f"));
params.add(new BasicNameValuePair("start_longitude", "78.3755f"));
params.add(new BasicNameValuePair("end_latitude", "17.3853f"));
params.add(new BasicNameValuePair("end_longitude","78.404f") );
params.add(new BasicNameValuePair("Authorization","Bearer"+token));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Language", "en_US");
httpPost.setHeader("Authorization","Bearer"+token);
httpPost.setEntity(new UrlEncodedFormEntity(params));
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();
Log.e("JSONStr", json);
} catch (Exception e) {
e.getMessage();
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// 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;
}
}
【问题讨论】:
-
为什么您要手动进行所有呼叫 uber 提供了同样的 sdk,您可以在其中找到估算骑行的方法,它也会减少您的工作
-
我认为
Authorization只是一个标题,而不是一个 POST 参数。再说一次,我没有阅读 API 文档。除此之外,我建议您查看 Retrofit 库(或实际的 Uber Android SDK) -
@cricket_007 uber sdk 正在做同样的事情,他们正在使用改造