I'm trying to make a RESTful API call to a Parse.com server using HTTPURLConnection. Unfortunately, when I run the code below, I get an error of HTTP 401 (Unauthorised Access)
Here are the codes
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;
Basically, all I need is to replicate the functionality of this 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
I ran the exact same cURL command with the same App ID and API key, and they are returning the correct result.
Any suggestions ??
EDIT:
Here is the console output I get:
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)
String urlReturned = httpsURLConnection.getURL().toString();to see if the url is not modified.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); } }and see if your authentication is correct.