【发布时间】:2015-09-24 04:07:33
【问题描述】:
我正在尝试使用 HTTPURLConnection 对 Parse.com 服务器进行 RESTful API 调用。不幸的是,当我运行下面的代码时,我收到 HTTP 401(未经授权的访问)错误
这里是代码
protected JSONObject doInBackground(String... params) {
HttpsURLConnection urlConnection = null;
try {
URL url = new URL("https://api.parse.com/1/functions/hello");
urlConnection =
(HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(20000);
urlConnection.setReadTimeout(20000);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("X-Parse-Application-Id", appId);
urlConnection.setRequestProperty("X-Parse-REST-API-Key", restApiKey);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();
byte[] outputBytes = "{}".getBytes("UTF-8");
OutputStream out = urlConnection.getOutputStream();
out.write(outputBytes);
out.close();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpURLConnection.HTTP_ACCEPTED) {
Log.d(TAG, "doInBackground(): connection failed: statusCode: " + statusCode);
// return null;
}
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
String responseText = getResponseText(in);
Log.d(TAG, "doInBackground() responseText: " + responseText);
return new JSONObject(responseText);
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
基本上,我只需要复制这个 cURL 的功能:
curl -X POST \
-H "X-Parse-Application-Id: {APP_ID}" \
-H "X-Parse-REST-API-Key: {API_KEY}" \
-H "Content-Type: application/json" \
-d '{}' \
https://api.parse.com/1/functions/hello
我使用相同的 App ID 和 API 密钥运行了完全相同的 cURL 命令,它们返回了正确的结果。
有什么建议吗??
编辑:
这是我得到的控制台输出:
09-24 14:28:43.101 21868-22057/com.example.versiontrack D/Module_VersionTracker﹕ doInBackground(): connection failed: statusCode: 401
09-24 14:28:43.102 21868-22057/com.example.versiontrack W/System.err﹕ java.io.FileNotFoundException: https://api.parse.com/1/functions/hello
09-24 14:28:43.102 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
09-24 14:28:43.103 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
09-24 14:28:43.103 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:215)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:115)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-24 14:28:43.105 21868-22057/com.example.versiontrack W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
【问题讨论】:
-
HttpURLConnection.HTTP_ACCEPTED = 202,你不想看 200 (HttpURLConnection.HTTP_OK) 吗?顺便说一句,您的代码似乎不错。 401 只能来自服务器。检查
String urlReturned = httpsURLConnection.getURL().toString();看url是否没有被修改。 -
网址是一样的。
-
所以你可以看到请求属性的输出:
Map<String, List<String>> map = httpURLConnection.getRequestProperties(); for (Map.Entry<String, List<String>> entry : map.entrySet()) { Log.d(TAG, "entry:" + entry.getKey()); for (String str : entry.getValue()) { Log.d(TAG, "value:" + str); } },看看你的身份验证是否正确。 -
所以我遇到了同样的问题,你解决了吗???
-
我做到了。代码工作正常。问题是我错误地复制粘贴了 API_KEY 而不是 Parse 中的 REST_API 密钥。
标签: android rest curl parse-platform